1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Carlos Antonio da Silva 2012-08-18 18:11:11 -07:00
commit f9fc26e800
3 changed files with 11 additions and 1 deletions

View file

@ -1,5 +1,9 @@
## Rails 4.0.0 (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.

View file

@ -574,7 +574,7 @@ module ActiveRecord
args.shift if args.first.is_a?(Hash) && args.first.empty?
collection = fetch_first_or_last_using_find?(args) ? scope : load_target
collection.send(type, *args)
collection.send(type, *args).tap {|it| set_inverse_instance it }
end
end
end

View file

@ -259,6 +259,12 @@ class InverseHasManyTests < ActiveRecord::TestCase
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.first.secret_interests }
end