Rails - How to choose the latest date in a table?

How to select the last date in a table where the column name is Lars.

I tried this:

<%= Reklamer.where(":dato => :dato, AND :name => :name", :date => Date.last, :name => "Lars")  %>

I tried this:

<%= Reklamer.where(name: 'Lars').order('dato ASC').limit(1).select('dato').inspect %>

Output: [#<Reklamer dato: "2011-02-15 23:53:28">]

I only need the datetime format, for example: 2011-02-15 23:53:28

How to do it?

+5
source share
1 answer

You need to place an order by date and select one entry:

<%= Reklamer.where(name: 'Lars').order('dato DESC').first %>

You can do this by limiting one entry:

Reklamer.where(name: 'Lars').order('dato DESC').limit(1)

If you need the latest date from the last entry, you can do this:

Reklamer.where(name: 'Lars').last.dato
+7
source

All Articles