From d37d40b2d702e9ff1461a3571f7f4d7ec52c2a2a 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. --- activerecord/lib/active_record/associations/collection_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 2c852f6efc..43701708a4 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -571,7 +571,7 @@ module ActiveRecord args.shift if args.first.is_a?(Hash) && args.first.empty? collection = fetch_first_or_last_using_find?(args) ? scoped : load_target - collection.send(type, *args).tap {|it| set_inverse_instance it } + collection.send(type, *args).tap { |it| set_inverse_instance it if args.blank? } end end end -- cgit v1.2.3