mirror of
https://github.com/github/rails.git
synced 2026-01-10 07:07:54 -05:00
Ensure calling first/last with options correctly set inverse association
Also related to #8087. Thanks @al2o3cr.
This commit is contained in:
@@ -571,7 +571,9 @@ 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 if args.blank? }
|
||||
collection.send(type, *args).tap do |record|
|
||||
set_inverse_instance record if record.is_a? ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -261,10 +261,18 @@ class InverseHasManyTests < ActiveRecord::TestCase
|
||||
|
||||
def test_parent_instance_should_be_shared_with_first_and_last_child
|
||||
man = Man.first
|
||||
|
||||
assert man.interests.first.man.equal? man
|
||||
assert man.interests.last.man.equal? man
|
||||
end
|
||||
|
||||
def test_parent_instance_should_be_shared_with_first_and_last_child_when_given_options
|
||||
man = Man.first
|
||||
|
||||
assert man.interests.first(:order => 'topic').man.equal? man
|
||||
assert man.interests.last(:order => 'topic').man.equal? man
|
||||
end
|
||||
|
||||
def test_parent_instance_should_be_shared_with_first_n_and_last_n_children
|
||||
man = Man.first
|
||||
|
||||
|
||||
Reference in New Issue
Block a user