aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-12-27 19:12:50 +0530
committerPratik Naik <pratiknaik@gmail.com>2009-12-27 19:12:50 +0530
commit352cc7c94a19f596b3ecb5f1a2b9712b1519e978 (patch)
tree1cab9881f464d9666dffdf880ef2f3b9f1c480c4
parentd5e98dc859e24e9ebf8206a7955c6ac40819a117 (diff)
downloadrails-352cc7c94a19f596b3ecb5f1a2b9712b1519e978.tar.gz
rails-352cc7c94a19f596b3ecb5f1a2b9712b1519e978.tar.bz2
rails-352cc7c94a19f596b3ecb5f1a2b9712b1519e978.zip
Make Model.find(:last) use relations
-rwxr-xr-xactiverecord/lib/active_record/base.rb47
1 files changed, 5 insertions, 42 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index ed031088a7..0d3ae1ef3e 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -640,18 +640,15 @@ module ActiveRecord #:nodoc:
# end
def find(*args)
options = args.extract_options!
- validate_find_options(options)
set_readonly_option!(options)
+ relation = construct_finder_arel_with_includes(options)
+
case args.first
- when :first
- construct_finder_arel_with_includes(options).first
- when :last
- find_last(options)
- when :all
- construct_finder_arel_with_includes(options).all
+ when :first, :last, :all
+ relation.send(args.first)
else
- construct_finder_arel_with_includes(options).find(*args)
+ relation.find(*args)
end
end
@@ -1513,40 +1510,6 @@ module ActiveRecord #:nodoc:
end
private
- def find_last(options)
- order = options[:order]
-
- if order
- order = reverse_sql_order(order)
- elsif !scoped?(:find, :order)
- order = "#{table_name}.#{primary_key} DESC"
- end
-
- if scoped?(:find, :order)
- scope = scope(:find)
- original_scoped_order = scope[:order]
- scope[:order] = reverse_sql_order(original_scoped_order)
- end
-
- begin
- construct_finder_arel_with_includes(options).order(order).first
- ensure
- scope[:order] = original_scoped_order if original_scoped_order
- end
- end
-
- def reverse_sql_order(order_query)
- order_query.to_s.split(/,/).each { |s|
- if s.match(/\s(asc|ASC)$/)
- s.gsub!(/\s(asc|ASC)$/, ' DESC')
- elsif s.match(/\s(desc|DESC)$/)
- s.gsub!(/\s(desc|DESC)$/, ' ASC')
- else
- s.concat(' DESC')
- end
- }.join(',')
- end
-
# Finder methods must instantiate through this method to work with the
# single-table inheritance model that makes it possible to create
# objects of different types from the same table.