I have 2 objects Weeklyactivity and Weeklyfood. Weeklyactivity has property called food_id. and Weeklyfood has 7 properties called mon, tue, …, sun.
When the user click update weekly food, the action method retrieves daily activity for a given week. It also retrieve all the weeklyfood object, which contains the id of food ate by the user every day of that week.
Weeklyactivities = Weeklyactivity.where(:week => wk)
@weeklyfood = Weeklyfood.New(forms[:weeklyfood])
Now I’d like to iterate over weeklyfood and transfert its data, which contains the id of the food the user ate each day of the week.
I don’t know if this is the right way to do it or even if there is a compact way of writing this code.
Weeklyactivities[:day => 'mon'].food_id = @weeklyfood[:mon]
Weeklyactivities[:day => 'tue'].food_id = @weeklyfood[:tue]
//and son ...
EDIT
I’m getting the following error: “can’t convert Hash into Integer…”
Not sure I understand the question.
You have a model
Weeklyactivitythat has attributesweekandday(and others)?And according to your code example you have already created rows in the database for all days in a week for
Weeklyactivity?Now you want to update these rows from a form?
In that case I have this suggestion:
And I think your objects
WeeklyactivityandWeeklyfoodshould be namedWeeklyActivityandWeeklyFoodwith tables respectivelyweekly_activitiesandweekly_foods