My goal is to return a single object in a Rails Active Record query. I want to retrieve the most recent record when the table is sorted by the created_at date. So, in the example below, that means I want to return the record with the ID of 2. I don't understand why the 4 queries below all return the same result?
Sales Table Records
+----+---------+-------+------------+----------------------------+
| ID | user_id | price | sale_date | created_at |
+----+---------+-------+------------+----------------------------+
| 1 | 1 | 200 | 2012-08-07 | 2012-10-02 23:01:46.706727 |
| 2 | 1 | 400 | 2009-05-11 | 2012-10-02 23:09:01.342879 |
+----+---------+-------+------------+----------------------------+
Queries:
<%= current_user.sales.order("created_at").last.price %>
returns: 200
<%= current_user.sales.order("created_at DESC").last.price %>
returns: 200
<%= current_user.sales.order("created_at ASC").last.price %>
returns: 200
<%= current_user.sales.last.price %>
returns: 200
I also tried:
<%= current_user.sales.last("created_at").price %>
returns: undefined method 'assert_valid_keys' for "created_at":String