While dmarkow's answer is technically correct, you need to create an index on created_at or subject an ever slower query as your database grows.
If you know that your id column is the primary key of auto increment (which is probably the case), just use it because it is an index by definition.
In addition, if AREL is not optimized to select only one entry in find (: last), you run the risk of causing it to select ALL records and then return only the last to you using the "last ()" method. More effectively limiting the results to one:
MyModel.last(:order => "id asc", :limit => 1)
or
MyModel.first(:order => "id desc", :limit => 1)
jemminger Feb 02 2018-11-11T00: 00Z
source share