Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - How to select the last date in a table?

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

I have tried this:

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

I have tried this:

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

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

I need just a datetime format like: 2011-02-15 23:53:28

How do I do that?

like image 636
Rails beginner Avatar asked Feb 18 '11 23:02

Rails beginner


1 Answers

You have to order by the date and select a single record:

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

You can accomplish this by limiting to a single record as well:

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

If you just want the last date from the last entry, you can do this:

Reklamer.where(name: 'Lars').last.dato
like image 80
Pan Thomakos Avatar answered Nov 15 '22 23:11

Pan Thomakos