我有一个具有以下结构的应用程序:
mealplan
一周中的每一天都包含一个recipe
,recipe
has_many
ingredients
,grocery
是一个项目在用户的购物清单上。
我的mealplan.rb
模型中有以下内容(警告:它不是很干。我先发制人的道歉):
class Mealplan < ActiveRecord::Base
belongs_to :user
def add_to_grocery_list(current_user)
@monday = Recipe.where(id: self.monday)[0]
@tuesday = Recipe.where(id: self.tuesday)[0]
@wednesday = Recipe.where(id: self.wednesday)[0]
@thursday = Recipe.where(id: self.thursday)[0]
@friday = Recipe.where(id: self.friday)[0]
@saturday = Recipe.where(id: self.saturday)[0]
@sunday = Recipe.where(id: self.sunday)[0]
@monday.ingredients.each do |ingredient|
if ingredient.listable
Grocery.new(name: ingredient.name, quantity: ingredient.quantity, user_id: current_user.id)
end
end
@tuesday.ingredients.each do |ingredient|
if ingredient.listable
Grocery.new(name: ingredient.name, quantity: ingredient.quantity, user_id: current_user.id)
end
end
@wednesday.ingredients.each do |ingredient|
if ingredient.listable
Grocery.new(name: ingredient.name, quantity: ingredient.quantity, user_id: current_user.id)
end
end
@thursday.ingredients.each do |ingredient|
if ingredient.listable
Grocery.new(name: ingredient.name, quantity: ingredient.quantity, user_id: current_user.id)
end
end
@friday.ingredients.each do |ingredient|
if ingredient.listable
Grocery.new(name: ingredient.name, quantity: ingredient.quantity, user_id: current_user.id)
end
end
@saturday.ingredients.each do |ingredient|
if ingredient.listable
Grocery.new(name: ingredient.name, quantity: ingredient.quantity, user_id: current_user.id)
end
end
@sunday.ingredients.each do |ingredient|
if ingredient.listable
Grocery.new(name: ingredient.name, quantity: ingredient.quantity, user_id: current_user.id)
end
end
end
end
然后我在我的mealplan#create
和mealplan#update
方法中调用此方法,如下所示:
def create
@mealplan = Mealplan.new(mealplan_params)
@mealplan.user_id = current_user.id
@mealplan.add_to_grocery_list(current_user)
if @mealplan.save
redirect_to mealplans_path, notice: 'Mealplan was successfully created.'
else
render :new
end
end
# PATCH/PUT /mealplans/1
def update
if @mealplan.update(mealplan_params)
@mealplan.add_to_grocery_list(current_user)
redirect_to mealplans_path, notice: 'Mealplan was successfully updated.'
else
render :edit
end
end
当我实际创建mealplan
时,我不会收到错误,但grocery
记录也不会得到create
d。这是服务器日志:
Started POST "/mealplans" for ::1 at 2017-08-18 13:27:09 -0700
Processing by MealplansController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Rm6Wuh93HnX5gwGhGDthS5b7DmY0IbIhKShu7hagFaZzeqmmxbHTj/ixTqZn3Q9InfpxGeUcXKQvApG2+hcV6A==", "mealplan"=>{"week_starting(1i)"=>"2017", "week_starting(2i)"=>"8", "week_starting(3i)"=>"18", "monday"=>"5", "tuesday"=>"5", "wednesday"=>"5", "thursday"=>"4", "friday"=>"4", "saturday"=>"4", "sunday"=>"4"}, "commit"=>"Create Mealplan"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Recipe Load (0.2ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? [["id", 5]]
CACHE (0.0ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? [["id", "5"]]
CACHE (0.0ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? [["id", "5"]]
Recipe Load (0.1ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? [["id", 4]]
CACHE (0.0ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? [["id", "4"]]
CACHE (0.0ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? [["id", "4"]]
CACHE (0.0ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? [["id", "4"]]
Ingredient Load (0.1ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" = ? [["recipe_id", 5]]
CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" = ? [["recipe_id", 5]]
CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" = ? [["recipe_id", 5]]
Ingredient Load (0.2ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" = ? [["recipe_id", 4]]
CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" = ? [["recipe_id", 4]]
CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" = ? [["recipe_id", 4]]
CACHE (0.0ms) SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" = ? [["recipe_id", 4]]
(0.0ms) begin transaction
SQL (0.4ms) INSERT INTO "mealplans" ("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", "week_starting", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["monday", "5"], ["tuesday", "5"], ["wednesday", "5"], ["thursday", "4"], ["friday", "4"], ["saturday", "4"], ["sunday", "4"], ["week_starting", "2017-08-18"], ["user_id", 2], ["created_at", "2017-08-18 20:27:09.248296"], ["updated_at", "2017-08-18 20:27:09.248296"]]
(0.8ms) commit transaction
Redirected to http://localhost:3000/mealplans
Completed 302 Found in 61ms (ActiveRecord: 3.5ms)
我在哪里错了,所以add_to_grocery_list
方法不起作用?我没有收到任何错误,但是没有创建新的grocery
项目。
答案 0 :(得分:1)
而不是Grocery.new
您可以说Grocery.create
。现在,您永远不会保存新的Grocery
记录。