aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRyan Bates <ryan@railscasts.com>2008-05-20 12:11:25 +0100
committerPratik Naik <pratiknaik@gmail.com>2008-05-20 12:27:14 +0100
commit73c59638549686fccc749ffd3ac53cb533c5fd61 (patch)
tree44aebe6752869fdb13240d0bf8422388cca81767 /activerecord/lib
parentebb642fa3a2b1a4e31abf9610ca634e6bb5d57d3 (diff)
downloadrails-73c59638549686fccc749ffd3ac53cb533c5fd61.tar.gz
rails-73c59638549686fccc749ffd3ac53cb533c5fd61.tar.bz2
rails-73c59638549686fccc749ffd3ac53cb533c5fd61.zip
Add first/last methods to associations/named_scope. [#226 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb25
-rw-r--r--activerecord/lib/active_record/named_scope.rb24
2 files changed, 47 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 7e47bf7bdf..2d3750e107 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -48,6 +48,26 @@ module ActiveRecord
end
end
+ # fetch first using SQL if possible
+ def first(*args)
+ if fetch_first_or_last_using_find? args
+ find(:first, *args)
+ else
+ load_target unless loaded?
+ @target.first(*args)
+ end
+ end
+
+ # fetch last using SQL if possible
+ def last(*args)
+ if fetch_first_or_last_using_find? args
+ find(:last, *args)
+ else
+ load_target unless loaded?
+ @target.last(*args)
+ end
+ end
+
def to_ary
load_target
@target.to_ary
@@ -330,7 +350,10 @@ module ActiveRecord
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
end
end
-
+
+ def fetch_first_or_last_using_find?(args)
+ args.first.kind_of?(Hash) || !(loaded? || @owner.new_record? || @reflection.options[:finder_sql] || !@target.blank? || args.first.kind_of?(Integer))
+ end
end
end
end
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 06edaed3d5..c7cf1731c1 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -102,7 +102,13 @@ module ActiveRecord
class Scope
attr_reader :proxy_scope, :proxy_options
- [].methods.each { |m| delegate m, :to => :proxy_found unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate)/ }
+
+ [].methods.each do |m|
+ unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|find|count|sum|average|maximum|minimum|paginate|first|last)/
+ delegate m, :to => :proxy_found
+ end
+ end
+
delegate :scopes, :with_scope, :to => :proxy_scope
def initialize(proxy_scope, options, &block)
@@ -115,6 +121,22 @@ module ActiveRecord
load_found; self
end
+ def first(*args)
+ if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
+ proxy_found.first(*args)
+ else
+ find(:first, *args)
+ end
+ end
+
+ def last(*args)
+ if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
+ proxy_found.last(*args)
+ else
+ find(:last, *args)
+ end
+ end
+
protected
def proxy_found
@found || load_found