Permalink
Browse files

Ensure calling first/last with options correctly set inverse association

Also related to #8087. Thanks @al2o3cr.
  • Loading branch information...
1 parent d37d40b commit c368b660be2433e291a2c87b3eb3de3dfac397ef @carlosantoniodasilva carlosantoniodasilva committed Nov 1, 2012
@@ -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

0 comments on commit c368b66

Please sign in to comment.