From 694334c37a95e85e686b121ce5cb2ccb4114e6b5 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 1 Nov 2012 13:14:58 -0200 Subject: Fix issue with collection associations and first(n)/last(n) When calling first(n) or last(n) in a collection, Active Record was improperly trying to set the inverse of instance in case that option existed. This change was introduced by fdf4eae506fa9895e831f569bed3c4aa6a999a22. In such cases we don't need to do that "manually", since the way collection will be loaded will already handle that, so we just skip setting the inverse association when any argument is given to first(n)/last(n). The test included ensures that these scenarios will have the inverse of instance set properly. Fixes #8087, Closes #8094. Squashed cherry-pick from d37d40b and c368b66. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/collection_association.rb --- activerecord/lib/active_record/associations/collection_association.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 7f39d3083e..54215cf88d 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -576,7 +576,9 @@ module ActiveRecord args.shift if args.first.is_a?(Hash) && args.first.empty? collection = fetch_first_or_last_using_find?(args) ? scope : load_target - collection.send(type, *args).tap {|it| set_inverse_instance it } + collection.send(type, *args).tap do |record| + set_inverse_instance record if record.is_a? ActiveRecord::Base + end end end end -- cgit v1.2.3