instance method
first
Ruby on Rails 4.0.13
Since v3.0.20Signature
first(limit = nil)
Find the first record (or first N records if a parameter is supplied). If no order is defined it will order by primary key.
Person.first # returns the first object fetched by SELECT * FROM people Person.where(["user_name = ?", user_name]).first Person.where(["user_name = :u", { u: user_name }]).first Person.order("created_on DESC").offset(5).first Person.first(3) # returns the first three objects fetched by SELECT * FROM people LIMIT 3
Parameters
-
limitopt = nil
Source
# File activerecord/lib/active_record/relation/finder_methods.rb, line 83
def first(limit = nil)
if limit
if order_values.empty? && primary_key
order(arel_table[primary_key].asc).limit(limit).to_a
else
limit(limit).to_a
end
else
find_first
end
end
Defined in activerecord/lib/active_record/relation/finder_methods.rb line 83
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::FinderMethods