aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-08-14 21:38:33 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-08-14 21:38:33 +0900
commit33d62981b667e72e25c38de848dbac843713f192 (patch)
treef3b6cab82ff2fe92888f12fdb4a56b723525e641 /activerecord/lib
parent6107a40c0e4d05614493bddf33d5ae8d9ce8a8d2 (diff)
downloadrails-33d62981b667e72e25c38de848dbac843713f192.tar.gz
rails-33d62981b667e72e25c38de848dbac843713f192.tar.bz2
rails-33d62981b667e72e25c38de848dbac843713f192.zip
`CollectionProxy#take` should respect dirty target
`#first`, `#second`, ..., `#last` methods respects dirty target. But `#take` doesn't respect it. This commit fixes the inconsistent behavior.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb22
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb4
2 files changed, 11 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index ab267f6897..44404cc176 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -147,11 +147,11 @@ module ActiveRecord
first_nth_or_last(:last, *args)
end
- def take(n = nil)
- if loaded?
- n ? target.take(n) : target.first
+ def take(limit = nil)
+ if find_from_target?
+ limit ? load_target.take(limit) : load_target.first
else
- scope.take(n)
+ scope.take(limit)
end
end
@@ -608,14 +608,10 @@ module ActiveRecord
# * target already loaded
# * owner is new record
# * target contains new or changed record(s)
- def fetch_first_nth_or_last_using_find?(args)
- if args.first.is_a?(Hash)
- true
- else
- !(loaded? ||
- owner.new_record? ||
- target.any? { |record| record.new_record? || record.changed? })
- end
+ def find_from_target?
+ loaded? ||
+ owner.new_record? ||
+ target.any? { |record| record.new_record? || record.changed? }
end
def include_in_memory?(record)
@@ -649,7 +645,7 @@ module ActiveRecord
def first_nth_or_last(type, *args)
args.shift if args.first.is_a?(Hash) && args.first.empty?
- collection = fetch_first_nth_or_last_using_find?(args) ? scope : load_target
+ collection = find_from_target? ? load_target : scope
collection.send(type, *args)
end
end
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index 806a905323..fb1b31429e 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -262,8 +262,8 @@ module ActiveRecord
# another_person_without.pets # => []
# another_person_without.pets.take # => nil
# another_person_without.pets.take(2) # => []
- def take(n = nil)
- @association.take(n)
+ def take(limit = nil)
+ @association.take(limit)
end
# Returns a new object of the collection type that has been instantiated