Permalink
Browse files

Add #delete support to has_many :through associations. Closes #6049 […

…Martin Landers]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5267 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
technoweenie committed Oct 9, 2006
1 parent 4e66f5b commit 850087f0bd691568690ca9d3169c88c0f3ad7c3b
View
@@ -1,5 +1,7 @@
*SVN*
+* Add #delete support to has_many :through associations. Closes #6049 [Martin Landers]
+
* Reverted old select_limited_ids_list postgresql fix that caused issues in mysql. Closes #5851 [Rick]
* Removes the ability for eager loaded conditions to be interpolated, since there is no model instance to use as a context for interpolation. #5553 [turnip@turnipspatch.com]
@@ -69,6 +69,24 @@ def <<(*records)
[:push, :concat].each { |method| alias_method method, :<< }
+ # Remove +records+ from this association. Does not destroy +records+.
+ def delete(*records)
+ records = flatten_deeper(records)
+ records.each { |associate| raise_on_type_mismatch(associate) }
+ records.reject! { |associate| @target.delete(associate) if associate.new_record? }
+ return if records.empty?
+
+ @delete_join_finder ||= "find_all_by_#{@reflection.source_reflection.association_foreign_key}"
+ through = @reflection.through_reflection
+ through.klass.transaction do
+ records.each do |associate|
+ joins = @owner.send(through.name).send(@delete_join_finder, associate.id)
+ @owner.send(through.name).delete(joins)
+ @target.delete(associate)
+ end
+ end
+ end
+
def build(attrs = nil)
raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, @reflection.through_reflection)
end
@@ -418,6 +418,39 @@ def test_adding_to_has_many_through_should_return_self
assert_equal tags, posts(:thinking).tags.push(tags(:general))
end
+ def test_delete_associate_when_deleting_from_has_many_through
+ count = posts(:thinking).tags.count
+ tags_before = posts(:thinking).tags
+ tag = Tag.create!(:name => 'doomed')
+ post_thinking = posts(:thinking)
+ post_thinking.tags << tag
+ assert_equal(count + 1, post_thinking.tags(true).size)
+
+ assert_nothing_raised { post_thinking.tags.delete(tag) }
+ assert_equal(count, post_thinking.tags.size)
+ assert_equal(count, post_thinking.tags(true).size)
+ assert_equal(tags_before.sort, post_thinking.tags.sort)
+ end
+
+ def test_delete_associate_when_deleting_from_has_many_through_with_multiple_tags
+ count = posts(:thinking).tags.count
+ tags_before = posts(:thinking).tags
+ doomed = Tag.create!(:name => 'doomed')
+ doomed2 = Tag.create!(:name => 'doomed2')
+ quaked = Tag.create!(:name => 'quaked')
+ post_thinking = posts(:thinking)
+ post_thinking.tags << doomed << doomed2
+ assert_equal(count + 2, post_thinking.tags(true).size)
+
+ assert_nothing_raised { post_thinking.tags.delete(doomed, doomed2, quaked) }
+ assert_equal(count, post_thinking.tags.size)
+ assert_equal(count, post_thinking.tags(true).size)
+ assert_equal(tags_before.sort, post_thinking.tags.sort)
+ end
+
+ def test_deleting_junk_from_has_many_through_should_raise_type_mismatch
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags.delete("Uhh what now?") }
+ end
def test_has_many_through_sum_uses_calculations
assert_nothing_raised { authors(:david).comments.sum(:post_id) }

0 comments on commit 850087f

Please sign in to comment.