Method: ActiveRecord::FinderMethods#first
- Defined in:
- activerecord/lib/active_record/relation/finder_methods.rb
#first(limit = nil) ⇒ Object
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 ORDER BY people.id LIMIT 1
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 ORDER BY people.id LIMIT 3
142 143 144 145 146 147 148 |
# File 'activerecord/lib/active_record/relation/finder_methods.rb', line 142 def first(limit = nil) if limit find_nth_with_limit(0, limit) else find_nth 0 end end |