Skip to content
This repository
Browse code

Added ActiveRecord#Base.all/first/last as aliases for find(:all/:firs…

…t/:last) (closes #11413) [nkallen, thechrisoshow]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9085 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit c6f2af5c31577e25c6f6b9f17be18858200a8e13 1 parent 081ddb6
David Heinemeier Hansson authored March 24, 2008
6  activerecord/CHANGELOG
... ...
@@ -1,10 +1,12 @@
1 1
 *SVN*
2 2
 
  3
+* Added ActiveRecord#Base.all/first/last as aliases for find(:all/:first/:last) #11413 [nkallen, thechrisoshow]
  4
+
3 5
 * Merge the has_finder gem, renamed as 'named_scope'.  #11404 [nkallen]
4 6
 
5 7
   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 => ...
8 10
   end
9 11
 
10 12
   Article.published.paginate(:page => 1)
12  activerecord/lib/active_record/base.rb
@@ -511,7 +511,19 @@ def find(*args)
511 511
           else             find_from_ids(args, options)
512 512
         end
513 513
       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
514 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
+      
515 527
       #
516 528
       # Executes a custom sql query against your database and returns all the results.  The results will
517 529
       # be returned as an array with columns requested encapsulated as attributes of the model you call
4  activerecord/test/cases/base_test.rb
@@ -1625,6 +1625,10 @@ def test_find_last
1625 1625
     assert_equal last, Developer.find(:first, :order => 'id desc')
1626 1626
   end
1627 1627
   
  1628
+  def test_last
  1629
+    assert_equal Developer.find(:first, :order => 'id desc'), Developer.last
  1630
+  end
  1631
+  
1628 1632
   def test_find_ordered_last
1629 1633
     last  = Developer.find :last, :order => 'developers.salary ASC'
1630 1634
     assert_equal last, Developer.find(:all, :order => 'developers.salary ASC').last
8  activerecord/test/cases/finder_test.rb
@@ -143,6 +143,14 @@ def test_find_first_failing
143 143
     first = Topic.find(:first, :conditions => "title = 'The First Topic!'")
144 144
     assert_nil(first)
145 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!'")
  153
+  end
146 154
 
147 155
   def test_unexisting_record_exception_handling
148 156
     assert_raises(ActiveRecord::RecordNotFound) {

0 notes on commit c6f2af5

Please sign in to comment.
Something went wrong with that request. Please try again.