Catmal Catmal - 1 month ago 8
MySQL Question

Rails show latest value after grouping

If I have this database table servers:

date, server_id, server_name, server_status

I have set in my controller:

@servers = Server.all.group(:server_id)


And assuming in my view I have a table like:

<% @servers.each do |server| %>

<td><%= server.server_name %></td>

<td><%= server.server_status %></td>


How do I show the server status at the most recent date recorded?

Answer
@servers = Server.order(created_at: :desc).group(:server_id)