提交 c368b660 编写于 作者: C Carlos Antonio da Silva

Ensure calling first/last with options correctly set inverse association

Also related to #8087. Thanks @al2o3cr.
上级 d37d40b2
......@@ -571,7 +571,9 @@ def first_or_last(type, *args)
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 @@ def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册