Changeset 9012
- Timestamp:
- 03/12/08 21:26:02 (1 year ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/base.rb (modified) (4 diffs)
- trunk/activerecord/test/cases/base_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r9003 r9012 1 1 *SVN* 2 3 * Added ActiveRecord::Base.find(:last) #11338 [miloops] 2 4 3 5 * test_native_types expects DateTime.local_offset instead of DateTime.now.offset; fixes test breakage due to dst transition [Geoff Buesing] trunk/activerecord/lib/active_record/base.rb
r9011 r9012 431 431 432 432 class << self # Class methods 433 # Find operates with threedifferent retrieval approaches:433 # Find operates with four different retrieval approaches: 434 434 # 435 435 # * Find by id: This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). 436 436 # If no record can be found for all of the listed ids, then RecordNotFound will be raised. 437 437 # * Find first: This will return the first record matched by the options used. These options can either be specific 438 # conditions or merely an order. If no record can be matched, nil is returned. 439 # * Find last: This will return the last record matched by the options used. These options can either be specific 438 440 # conditions or merely an order. If no record can be matched, nil is returned. 439 441 # * Find all: This will return all the records matched by the options used. If no records are found, an empty array is returned. … … 476 478 # Person.find(:first, :order => "created_on DESC", :offset => 5) 477 479 # 480 # Examples for find last: 481 # Person.find(:last) # returns the last object fetched by SELECT * FROM people 482 # Person.find(:last, :conditions => [ "user_name = ?", user_name]) 483 # Person.find(:last, :order => "created_on DESC", :offset => 5) 484 # 478 485 # Examples for find all: 479 486 # Person.find(:all) # returns an array of objects for all the rows fetched by SELECT * FROM people … … 500 507 case args.first 501 508 when :first then find_initial(options) 509 when :last then find_last(options) 502 510 when :all then find_every(options) 503 511 else find_from_ids(args, options) … … 1237 1245 end 1238 1246 1247 def find_last(options) 1248 order = options[:order] 1249 1250 if order 1251 order = reverse_sql_order(order) 1252 elsif !scoped?(:find, :order) 1253 order = "#{table_name}.#{primary_key} DESC" 1254 end 1255 1256 if scoped?(:find, :order) 1257 scoped_order = reverse_sql_order(scope(:find, :order)) 1258 scoped_methods.select { |s| s[:find].update(:order => scoped_order) } 1259 end 1260 1261 find_initial(options.merge({ :order => order })) 1262 end 1263 1264 def reverse_sql_order(order_query) 1265 reversed_query = order_query.split(/,/).each { |s| 1266 if s.match(/\s(asc|ASC)$/) 1267 s.gsub!(/\s(asc|ASC)$/, ' DESC') 1268 elsif s.match(/\s(desc|DESC)$/) 1269 s.gsub!(/\s(desc|DESC)$/, ' ASC') 1270 elsif !s.match(/\s(asc|ASC|desc|DESC)$/) 1271 s.concat(' DESC') 1272 end 1273 }.join(',') 1274 end 1275 1239 1276 def find_every(options) 1240 1277 include_associations = merge_includes(scope(:find, :include), options[:include]) trunk/activerecord/test/cases/base_test.rb
r8913 r9012 1617 1617 end 1618 1618 1619 def test_find_last 1620 last = Developer.find :last 1621 assert_equal last, Developer.find(:first, :order => 'id desc') 1622 end 1623 1624 def test_find_ordered_last 1625 last = Developer.find :last, :order => 'developers.salary ASC' 1626 assert_equal last, Developer.find(:all, :order => 'developers.salary ASC').last 1627 end 1628 1629 def test_find_reverse_ordered_last 1630 last = Developer.find :last, :order => 'developers.salary DESC' 1631 assert_equal last, Developer.find(:all, :order => 'developers.salary DESC').last 1632 end 1633 1634 def test_find_multiple_ordered_last 1635 last = Developer.find :last, :order => 'developers.name, developers.salary DESC' 1636 assert_equal last, Developer.find(:all, :order => 'developers.name, developers.salary DESC').last 1637 end 1638 1639 def test_find_scoped_ordered_last 1640 last_developer = Developer.with_scope(:find => { :order => 'developers.salary ASC' }) do 1641 Developer.find(:last) 1642 end 1643 assert_equal last_developer, Developer.find(:all, :order => 'developers.salary ASC').last 1644 end 1645 1619 1646 def test_abstract_class 1620 1647 assert !ActiveRecord::Base.abstract_class?