diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2008-03-24 19:59:22 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-03-24 19:59:22 +0000 |
commit | c6f2af5c31577e25c6f6b9f17be18858200a8e13 (patch) | |
tree | 4b5835b922381231aca10b5b1235d4a555a3af83 /activerecord | |
parent | 081ddb6f24d70794ee9e071d3ed5302f52b26c4d (diff) | |
download | rails-c6f2af5c31577e25c6f6b9f17be18858200a8e13.tar.gz rails-c6f2af5c31577e25c6f6b9f17be18858200a8e13.tar.bz2 rails-c6f2af5c31577e25c6f6b9f17be18858200a8e13.zip |
Added ActiveRecord#Base.all/first/last as aliases for find(:all/:first/:last) (closes #11413) [nkallen, thechrisoshow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9085 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 6 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 12 | ||||
-rwxr-xr-x | activerecord/test/cases/base_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 8 |
4 files changed, 28 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 96075d0d33..d4dccb08b1 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,10 +1,12 @@ *SVN* +* Added ActiveRecord#Base.all/first/last as aliases for find(:all/:first/:last) #11413 [nkallen, thechrisoshow] + * Merge the has_finder gem, renamed as 'named_scope'. #11404 [nkallen] class Article < ActiveRecord::Base - has_finder :published, :conditions => {:published => true} - has_finder :popular, :conditions => ... + named_scope :published, :conditions => {:published => true} + named_scope :popular, :conditions => ... end Article.published.paginate(:page => 1) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 9716473a4e..4f9fd71ffc 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -511,7 +511,19 @@ module ActiveRecord #:nodoc: else find_from_ids(args, options) end end + + # This is an alias for find(:first). You can pass in all the same arguments to this method as you can + # to find(:first) + def first(*args) + find(:first, *args) + end + # This is an alias for find(:last). You can pass in all the same arguments to this method as you can + # to find(:last) + def last(*args) + find(:last, *args) + end + # # Executes a custom sql query against your database and returns all the results. The results will # be returned as an array with columns requested encapsulated as attributes of the model you call diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index d8b107177f..025858bf15 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1625,6 +1625,10 @@ class BasicsTest < ActiveRecord::TestCase assert_equal last, Developer.find(:first, :order => 'id desc') end + def test_last + assert_equal Developer.find(:first, :order => 'id desc'), Developer.last + end + def test_find_ordered_last last = Developer.find :last, :order => 'developers.salary ASC' assert_equal last, Developer.find(:all, :order => 'developers.salary ASC').last diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 5da8603070..8cb5c9206a 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -143,6 +143,14 @@ class FinderTest < ActiveRecord::TestCase first = Topic.find(:first, :conditions => "title = 'The First Topic!'") assert_nil(first) end + + def test_first + assert_equal topics(:second).title, Topic.first(:conditions => "title = 'The Second Topic of the day'").title + end + + def test_first_failing + assert_nil Topic.first(:conditions => "title = 'The Second Topic of the day!'") + end def test_unexisting_record_exception_handling assert_raises(ActiveRecord::RecordNotFound) { |