diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-11-01 13:14:58 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-11-01 14:37:12 -0200 |
commit | 694334c37a95e85e686b121ce5cb2ccb4114e6b5 (patch) | |
tree | 7b5567bf74b061d8dfbb08cdc990c2dfa3213fd1 /activerecord/lib/active_record | |
parent | 69cebae3b2201ac3c31dca8f6f29a2557831505f (diff) | |
download | rails-694334c37a95e85e686b121ce5cb2ccb4114e6b5.tar.gz rails-694334c37a95e85e686b121ce5cb2ccb4114e6b5.tar.bz2 rails-694334c37a95e85e686b121ce5cb2ccb4114e6b5.zip |
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
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 4 |
1 files changed, 3 insertions, 1 deletions
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 |