aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG6
-rwxr-xr-xactiverecord/lib/active_record/base.rb12
-rwxr-xr-xactiverecord/test/cases/base_test.rb4
-rw-r--r--activerecord/test/cases/finder_test.rb8
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) {