I have two models: School and Review.
The School model looks like this: {ID, name, city, state}
The Review model looks like this: {ID, content, score, school_id}
How do I list the top ten schools based on the score from the review model?
I thought maybe a method in the school-model, with something like this:
class School < ActiveRecord::Base
def top_schools
@top_schools = School.limit(10)
...
end
end
And then loop them in a <li> list:
<div>
<ul>
<% @top_schools.each do |school| %>
<li>school.name</li>
<%end>
</ul>
</div>
But, I dont really know how to finish the top_schools method.
You should make an average of reviews of each school.
The SQL query if you are running with MySQL should be something like:
Translated in Rails:
In your School model:
In your controller:
The choice not to include the limitation in scope, can be more flexible and allow the display of 5 or 15.
I have only tested MySQL request. I am not sure on my rails translation.