diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-03-02 09:38:27 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-03-02 09:38:27 -0700 |
commit | e20dc1b313303540b44effd94fbc8cf39121ad28 (patch) | |
tree | 64841519da3cca0ff5f66edf8720cc6d90a36593 /activerecord | |
parent | c0584ea0346062982cfe3c776c826ea1b026a8ac (diff) | |
parent | 243cb81010dbf30ab99d6682a758048bfcb24bc7 (diff) | |
download | rails-e20dc1b313303540b44effd94fbc8cf39121ad28.tar.gz rails-e20dc1b313303540b44effd94fbc8cf39121ad28.tar.bz2 rails-e20dc1b313303540b44effd94fbc8cf39121ad28.zip |
Merge pull request #19105 from amatsuda/array_take
Preserve Array#take(n) behaviour of HasManyAssociation
Diffstat (limited to 'activerecord')
3 files changed, 18 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 82a7c27799..0ba03338f6 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -129,11 +129,11 @@ module ActiveRecord first_nth_or_last(:last, *args) end - def take + def take(n = nil) if loaded? - target.first + n ? target.take(n) : target.first else - scope.take.tap do |record| + scope.take(n).tap do |record| set_inverse_instance record if record.is_a? ActiveRecord::Base end end diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 87e80e88b2..e11c9490b7 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -227,8 +227,8 @@ module ActiveRecord @association.last(*args) end - def take - @association.take + def take(n = nil) + @association.take(n) end # Returns a new object of the collection type that has been instantiated diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 897c52a49d..675bed9bfa 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -478,6 +478,19 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_raise(ActiveRecord::RecordNotFound) { authors(:bob).posts.take! } end + def test_taking_with_a_number + # taking from unloaded Relation + bob = Author.find(authors(:bob).id) + assert_equal [posts(:misc_by_bob)], bob.posts.take(1) + bob = Author.find(authors(:bob).id) + assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], bob.posts.take(2) + + # taking from loaded Relation + bob.posts.to_a + assert_equal [posts(:misc_by_bob)], authors(:bob).posts.take(1) + assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], authors(:bob).posts.take(2) + end + def test_taking_with_inverse_of interests(:woodsmanship).destroy interests(:survival).destroy |