diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-09-28 17:22:01 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-09-28 17:22:01 -0700 |
commit | 54c05acdba138f3a7a3d44dfc922b0fe4e4cf554 (patch) | |
tree | 47fc4839934b50dba846ea56f0851bbd23d23730 /activerecord | |
parent | 50a96446bce10232bc70a7f4ef1c2d5f2018914e (diff) | |
parent | fed6ac9c66e87d120098b3a4ffb15699d0ee6863 (diff) | |
download | rails-54c05acdba138f3a7a3d44dfc922b0fe4e4cf554.tar.gz rails-54c05acdba138f3a7a3d44dfc922b0fe4e4cf554.tar.bz2 rails-54c05acdba138f3a7a3d44dfc922b0fe4e4cf554.zip |
Merge pull request #12375 from arthurnn/inverse_after_find_or_initialize
Inverse after find or initialize
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 11 |
3 files changed, 22 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 1fa891adc7..cf1f20c829 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,10 @@ ## unreleased ## +* 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. + + *arthurnn* + * Callbacks on has_many should access the in memory parent if a inverse_of is set. *arthurnn* diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 416a5823c6..62cdb1ea87 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -77,10 +77,12 @@ module ActiveRecord def method_missing(method, *args, &block) match = DynamicFinderMatch.match(method) if match && match.instantiator? - send(:find_or_instantiator_by_attributes, match, match.attribute_names, *args) do |r| - proxy_association.send :set_owner_attributes, r - proxy_association.send :add_to_target, r - yield(r) if block_given? + send(:find_or_instantiator_by_attributes, match, match.attribute_names, *args) do |record| + proxy_association.send :set_owner_attributes, record + proxy_association.send :add_to_target, record + yield(record) if block_given? + end.tap do |record| + proxy_association.send :set_inverse_instance, record end elsif target.respond_to?(method) || (!proxy_association.klass.respond_to?(method) && Class.respond_to?(method)) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index eec78f95c5..53cce33698 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -662,6 +662,17 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + def test_inverse_after_find_or_initialize + firm = companies(:first_firm) + client = firm.clients_of_firm.find_or_initialize_by_client_of(firm.id) + assert_no_queries do + assert_equal firm, client.firm + end + + firm.name = "A new firm" + assert_equal firm.name, client.firm.name + end + def test_new_aliased_to_build company = companies(:first_firm) new_client = assert_no_queries { company.clients_of_firm.new("name" => "Another Client") } |