Permalink
Browse files

Merge pull request #7377 from brainopia/use_inversed_parent_for_first…

…_and_last_child

Use inversed parent for first and last child of has_many association

[Backport] Closes #3223.

Conflicts:
	activerecord/lib/active_record/associations/collection_association.rb
  • Loading branch information...
1 parent b3bea49 commit fdf4eae506fa9895e831f569bed3c4aa6a999a22 @carlosantoniodasilva carlosantoniodasilva committed Aug 19, 2012
@@ -1,5 +1,9 @@
## Rails 3.2.9 (unreleased)
+* Use inversed parent for first and last child of has_many association.
+
+ *Ravil Bayramgalin*
+
* Fix Column.microseconds and Column.fast_string_to_date to avoid converting
timestamp seconds to a float, since it occasionally results in inaccuracies
with microsecond-precision times. Fixes #7352.
@@ -569,7 +569,7 @@ 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)
+ collection.send(type, *args).tap {|it| set_inverse_instance it }
end
end
end
@@ -259,6 +259,12 @@ def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
end
+ 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_trying_to_use_inverses_that_dont_exist_should_raise_an_error
assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.find(:first).secret_interests }
end

0 comments on commit fdf4eae

Please sign in to comment.