Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9085

Show
Ignore:
Timestamp:
03/24/08 19:59:22 (2 months ago)
Author:
david
Message:

Added ActiveRecord#Base.all/first/last as aliases for find(:all/:first/:last) (closes #11413) [nkallen, thechrisoshow]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r9084 r9085  
    11*SVN* 
    22 
     3* Added ActiveRecord#Base.all/first/last as aliases for find(:all/:first/:last) #11413 [nkallen, thechrisoshow] 
     4 
    35* Merge the has_finder gem, renamed as 'named_scope'.  #11404 [nkallen] 
    46 
    57  class Article < ActiveRecord::Base 
    6     has_finder :published, :conditions => {:published => true} 
    7     has_finder :popular, :conditions => ... 
     8    named_scope :published, :conditions => {:published => true} 
     9    named_scope :popular, :conditions => ... 
    810  end 
    911 
  • trunk/activerecord/lib/active_record/base.rb

    r9082 r9085  
    512512        end 
    513513      end 
    514  
     514       
     515      # This is an alias for find(:first).  You can pass in all the same arguments to this method as you can 
     516      # to find(:first) 
     517      def first(*args) 
     518        find(:first, *args) 
     519      end 
     520 
     521      # This is an alias for find(:last).  You can pass in all the same arguments to this method as you can 
     522      # to find(:last) 
     523      def last(*args) 
     524        find(:last, *args) 
     525      end 
     526       
    515527      # 
    516528      # Executes a custom sql query against your database and returns all the results.  The results will 
  • trunk/activerecord/test/cases/base_test.rb

    r9084 r9085  
    16261626  end 
    16271627   
     1628  def test_last 
     1629    assert_equal Developer.find(:first, :order => 'id desc'), Developer.last 
     1630  end 
     1631   
    16281632  def test_find_ordered_last 
    16291633    last  = Developer.find :last, :order => 'developers.salary ASC' 
  • trunk/activerecord/test/cases/finder_test.rb

    r9084 r9085  
    143143    first = Topic.find(:first, :conditions => "title = 'The First Topic!'") 
    144144    assert_nil(first) 
     145  end 
     146   
     147  def test_first 
     148    assert_equal topics(:second).title, Topic.first(:conditions => "title = 'The Second Topic of the day'").title 
     149  end 
     150   
     151  def test_first_failing 
     152    assert_nil Topic.first(:conditions => "title = 'The Second Topic of the day!'") 
    145153  end 
    146154