From 0e1cafcbc4d67854faf35e489571bc30f5e2ac14 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 1 Aug 2012 23:14:29 +0100 Subject: Add CollectionProxy#scope This can be used to get a Relation from an association. Previously we had a #scoped method, but we're deprecating that for AR::Base, so it doesn't make sense to have it here. This was requested by DHH, to facilitate code like this: Project.scope.order('created_at DESC').page(current_page).tagged_with(@tag).limit(5).scoping do @topics = @project.topics.scope @todolists = @project.todolists.scope @attachments = @project.attachments.scope @documents = @project.documents.scope end --- activerecord/lib/active_record/associations/collection_proxy.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 65a5044a3d..3a7a488302 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -855,7 +855,8 @@ module ActiveRecord @association.scoped.scoping { yield } end - def spawn + # Returns a Relation object for the records in this association + def scope association = @association @association.scoped.extending! do @@ -863,6 +864,9 @@ module ActiveRecord end end + # :nodoc: + alias spawn scope + # Equivalent to Array#==. Returns +true+ if the two arrays # contain the same number of elements and if each element is equal # to the corresponding element in the other array, otherwise returns -- cgit v1.2.3