I have an app with the following structure:
- A
mealplanincludes onerecipefor each day of the week. - A
recipehas_manyingredients. - A
groceryis one item on the user's grocery list.
I want to create a custom method so that when a button is clicked, it runs Grocery.create on each ingredient from the recipes on the mealplan.
I currently have the following mealplans#index method, so you can see how they're defined. (All of this is happening on the index view:
def index
@mealplans = Mealplan.where(user_id: current_user.id)
@mealplan = Mealplan.new
@recent = Mealplan.where(user_id: current_user.id).where("created_at > ?", Time.now.beginning_of_week).order("week_starting").last
@recipes = Recipe.where(user_id: current_user.id)
@monday = Recipe.where(id: @recent.monday)[0] if @recent.present?
@tuesday = Recipe.where(id: @recent.tuesday)[0] if @recent.present?
@wednesday = Recipe.where(id: @recent.wednesday)[0] if @recent.present?
@thursday = Recipe.where(id: @recent.thursday)[0] if @recent.present?
@friday = Recipe.where(id: @recent.friday)[0] if @recent.present?
@saturday = Recipe.where(id: @recent.saturday)[0] if @recent.present?
@sunday = Recipe.where(id: @recent.sunday)[0] if @recent.present?
end
I also have a dummy mealplans#add_to_list method set up in the controller, but I feel like doing it this way violates the "skinny controllers, fat models" principle of rails.
Can anyone clue me in to the "railsiest" way to accomplish this task, according to best practices?