aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-09-30 14:38:58 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-09-30 14:38:58 -0700
commitccd11d58910059f07b28cc518dbdad42cbc8ea0c (patch)
treea8cf48bfaa9e85439854f89e5cb675df63374740
parent54c05acdba138f3a7a3d44dfc922b0fe4e4cf554 (diff)
parent679860400f1c6c542b33e8c80221cc5aca7b5629 (diff)
downloadrails-ccd11d58910059f07b28cc518dbdad42cbc8ea0c.tar.gz
rails-ccd11d58910059f07b28cc518dbdad42cbc8ea0c.tar.bz2
rails-ccd11d58910059f07b28cc518dbdad42cbc8ea0c.zip
Merge pull request #12413 from arthurnn/inverse_of_on_build
Inverse of on build
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/associations/association.rb1
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb1
-rw-r--r--activerecord/test/cases/associations/inverse_associations_test.rb4
4 files changed, 10 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index cf1f20c829..5107e43b55 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,4 +1,10 @@
## unreleased ##
+* Move .set_inverse_instance call to association.build_record method. Everytime a new record is build
+ using the association, we need to try to set the inverse_of relation.
+
+ Fixes #10371.
+
+ *arthurnn*
* When calling the method .find_or_initialize_by_* from a collection_proxy
it should set the inverse_of relation even when the entry was found on the db.
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index 99f307922e..1e324c7e95 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -240,6 +240,7 @@ module ActiveRecord
skip_assign = [reflection.foreign_key, reflection.type].compact
attributes = create_scope.except(*(record.changed - skip_assign))
record.assign_attributes(attributes, :without_protection => true)
+ set_inverse_instance(record)
end
end
end
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index baddb9852f..c2dd462b5d 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -350,7 +350,6 @@ module ActiveRecord
end
callback(:after_add, record)
- set_inverse_instance(record)
record
end
diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb
index 0cab6faa25..51e466ecac 100644
--- a/activerecord/test/cases/associations/inverse_associations_test.rb
+++ b/activerecord/test/cases/associations/inverse_associations_test.rb
@@ -125,8 +125,10 @@ class InverseHasOneTests < ActiveRecord::TestCase
end
def test_parent_instance_should_be_shared_with_newly_created_child
- m = Man.find(:first)
+ m = Man.create
f = m.create_face(:description => 'haunted')
+
+ assert_equal m.object_id, f.man.object_id
assert_not_nil f.man
assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
m.name = 'Bongo'