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

Ticket #11413: add_first_and_last_aliases.diff

File add_first_and_last_aliases.diff, 2.3 kB (added by thechrisoshow, 6 months ago)

Adds alias methods first and last for find(:first) and find(:last) docs and tests included

  • activerecord/test/cases/base_test.rb

    old new  
    16251625    assert_equal last, Developer.find(:first, :order => 'id desc') 
    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' 
    16301634    assert_equal last, Developer.find(:all, :order => 'developers.salary ASC').last 
  • activerecord/test/cases/finder_test.rb

    old new  
    143143    first = Topic.find(:first, :conditions => "title = 'The First Topic!'") 
    144144    assert_nil(first) 
    145145  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!'") 
     153  end 
    146154 
    147155  def test_unexisting_record_exception_handling 
    148156    assert_raises(ActiveRecord::RecordNotFound) { 
  • activerecord/lib/active_record/base.rb

    old new  
    511511          else             find_from_ids(args, options) 
    512512        end 
    513513      end 
     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 
    514520 
     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 
    517529      # be returned as an array with columns requested encapsulated as attributes of the model you call