mirror of https://github.com/rails/rails
Made `first` finder consistent among database engines by adding a
default order clause (fixes #5103)
This commit is contained in:
parent
5603050656
commit
07e5301e69
|
@ -60,6 +60,9 @@ module ActiveRecord
|
|||
where(*args).first!
|
||||
end
|
||||
|
||||
# Find the first record (or first N records if a parameter is supplied).
|
||||
# If no order is defined it will order by primary key.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# Person.first # returns the first object fetched by SELECT * FROM people
|
||||
|
@ -67,7 +70,15 @@ module ActiveRecord
|
|||
# Person.where(["user_name = :u", { :u => user_name }]).first
|
||||
# Person.order("created_on DESC").offset(5).first
|
||||
def first(limit = nil)
|
||||
limit ? limit(limit).to_a : find_first
|
||||
if limit
|
||||
if order_values.empty? && primary_key
|
||||
order("#{quoted_table_name}.#{quoted_primary_key} ASC").limit(limit).to_a
|
||||
else
|
||||
limit(limit).to_a
|
||||
end
|
||||
else
|
||||
find_first
|
||||
end
|
||||
end
|
||||
|
||||
# Same as +first+ but raises <tt>ActiveRecord::RecordNotFound</tt> if no record
|
||||
|
@ -319,7 +330,12 @@ module ActiveRecord
|
|||
if loaded?
|
||||
@records.first
|
||||
else
|
||||
@first ||= limit(1).to_a[0]
|
||||
@first ||=
|
||||
if order_values.empty? && primary_key
|
||||
order("#{quoted_table_name}.#{quoted_primary_key} ASC").limit(1).to_a[0]
|
||||
else
|
||||
limit(1).to_a[0]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -163,6 +163,12 @@ class FinderTest < ActiveRecord::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_first_have_primary_key_order_by_default
|
||||
expected = topics(:first)
|
||||
expected.touch # PostgreSQL changes the default order if no order clause is used
|
||||
assert_equal expected, Topic.first
|
||||
end
|
||||
|
||||
def test_model_class_responds_to_first_bang
|
||||
assert Topic.first!
|
||||
Topic.delete_all
|
||||
|
|
Loading…
Reference in New Issue