I am new to rails, and Im having hard time writing a query using rails’ methods.
What I am trying to accomplish is to have a Canteen model that has many Meals, however I want to only display meals that are being served that day.
So I created a Canteen model, that has_many Meals.
Then I created Served_date model, that belongs_to meal and Meal has_many Served_dates so we can specify multiple dates when the meal is being served.
How would I make query like this?
//P.S.: Served_date.where(served_at: Date.today) returns Served_dates that are being served today
Thanks !
Firstly, the model name should not be underscored by convention, hence you should use ServedDate instead of Served_date.
This query should give you what you need:
Better yet, you can parameterize it and turn it into a scope to be more portable:
And then call: