Ensure calling first/last with options correctly set inverse association

Also related to #8087. Thanks @al2o3cr.
This commit is contained in:
Carlos Antonio da Silva
2012-11-01 14:27:45 -02:00
parent d37d40b2d7
commit c368b660be
2 changed files with 11 additions and 1 deletions

View File

@@ -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

View File

@@ -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