Search code examples
ruby-on-railsrubyactiverecord

Find last created_at record


How can I find the last record by their created_at field?

Record.order("created_at").last

or

Record.where(:created_at).last

or

Neither and there is a better way?


Solution

  • That's a valid request

    Record.order(created_at: :desc).first
    

    take care of a tree index on this field to have a better performance, though