From 96f63cb1700e5004e49ca2fb22987cee951c6d21 Mon Sep 17 00:00:00 2001 From: Rebecca Frankel Date: Sun, 9 Aug 2009 23:06:21 -0400 Subject: [PATCH] An after-create callback and test suggested by barunio in ticket 2765 --- .../has_many_through_associations_test.rb | 9 +++++++++ activerecord/test/models/tag.rb | 11 ++++++++++- 2 files changed, 19 insertions(+), 1 deletions(-) diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index a43f876..8b3eafc 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -321,4 +321,13 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase lambda { authors(:david).very_special_comments.delete(authors(:david).very_special_comments.first) }, ].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) } end + + + def test_after_create_callback_that_creates_a_link_between_two_objects_that_have_a_polymorphic_has_many_through_association + author = authors(:mary) + post = Post.create!(:author => author, :title => "TITLE", :body => "BODY") + tag = Tag.create! + assert_equal tag.taggings.length, 1 + end + end diff --git a/activerecord/test/models/tag.rb b/activerecord/test/models/tag.rb index a581b38..b11a483 100644 --- a/activerecord/test/models/tag.rb +++ b/activerecord/test/models/tag.rb @@ -4,4 +4,13 @@ class Tag < ActiveRecord::Base has_one :tagging has_many :tagged_posts, :through => :taggings, :source => :taggable, :source_type => 'Post' -end \ No newline at end of file + + after_create :link_tag_to_last_post + + def link_tag_to_last_post + if p = Post.last + Tagging.create!(:tag => self, :taggable => p) + end + end + +end -- 1.6.0.4