我有一個菜桌,一個配料表和一個dish_成分表來加入菜餚和配料(見下文)。每次我添加一個配料到菜,我想增加或添加配料的數量dish_ingredient表。我已經設法通過這樣做來更新號碼:有沒有一種簡單的方法將數據添加到Rails中的關係表中?
dish.dish_ingredients[counter].number_of_ingredients = new number
但通過這樣做,我需要管理一個計數器。有沒有簡單的方法在Rails中做到這一點?
class Dish < ActiveRecord::Base
has_many :dish_ingredients
has_many :ingredients, :through => :dish_ingredients
end
class Ingredient < ActiveRecord::Base
has_many :dish_ingredients
has_many :dishes, :through => :dish_ingredients
end
class DishIngredient < ActiveRecord::Base
belongs_to :dish
belongs_to :ingredient
end
# in the database table dish_ingredient contains a field 'number_of_ingredients'
# that I want to update when ingredients are modified or added to dish.
這是否解決您的問題? – emrass
不,這不是我要找的。我需要能夠在關聯表上設置一個值,而不只是增加或減少值。 – Fossmo