I've got a query in SQL:
SELECT Foreign_id, Type, COUNT(Type) FROM events GROUP BY Type, Foreign_id
Foreign_id is, obviously, the foreign key (and primary key of another table), and Type is a descriptor of the event type. This query, when run in the SQL client I am using on my MYSQL server, returns exactly what I am looking for.
I am wondering the most efficient way to run this in a Rails controller (there is a massive number of records). Right now I have no idea how to turn this into a single command in Rails. Here is what I have in my controller:
@foreign = Foreign.all
I then use this in the view:
<% foreign.each do |foreign| %>
<%= foreign.events.select{|event| event.Type == 'A'}.size %><br />
<%= foreign.events.select{|event| event.Type == 'B'}.size %>
...
<% end %>
I know there is a way more efficient way to get that number. Can anybody help me out?