I need help with a query. I have multiple Canteens, where each has multiple Meals, where each meal has multiple MealPicks.
Although I don’t know if this MealPick model is good idea, because I need to display how many times has the meal been picked TODAY, so I needed the timestamp to make this query.
class Meal < ActiveRecord::Base
def todays_picks
meal_picks.where(["created_at >= ? AND created_at < ?", Date.today.beginning_of_day, Date.today.end_of_day])
end
end
Before I had just a meal_picked_count counter in Meal which I incremented by increment_counter method.
Okay so, now I need to display for each Canteen the Meal that has the most MealPicks, I played around in the console and tried something like Canteen.find(1).meals.maximum("meal_picks.count") but that obviously does not work as it is not a column.
Any ideas?
1 Answer