我有一个has_many through连接表设置为配方应用程序在哪里Ingredient和Meal连接MealIngredient.在MealIngredient,我有meal_id,ingredient_id和amount.我的问题是:我如何访问该amount列?
在我的食谱视图中,我遍历了各种成分:
@meal.ingredients.each do |i|
Run Code Online (Sandbox Code Playgroud)
我可以访问成分的属性,但不能访问MealIngredient连接记录中的金额.
我尝试includes在查询中使用@meal.ingredients.includes(:meal_ingredients),但我不确定如何访问amount前面提到的循环.当我使用时i.inspect,我根本看不到任何对该meal_ingredients表的引用.
是否有某种方法可以使用该循环访问变量i.amount?
预先感谢您的任何帮助!
我是编程和轨道的新手,有一些我不完全理解的东西.我正在创建一个应用程序
product has_many categories
category has_many products
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,我需要创建一个具有product_id&a 的连接表products_categories category_id.首先,我还需要这个表的模型吗?如果是的话,我想它会是这样的:
class CategoryProduct < ActiveRecord::Base
belongs_to :category
belongs_to :product
end
Run Code Online (Sandbox Code Playgroud)
和product.rb中的其他模型:
class Product < ActiveRecord::Base
has_many :category_products
has_many :categories, through: :category_product
has_attached_file :picture,
styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :picture,
content_type: /\Aimage\/.*\z/
validates :price, presence: { message: "Merci d'indiquer le prix du produit" }
validates :name, presence: { message: "Merci d'indiquer le nom du produit" }
validates :weight, presence: { message: "Merci d'indiquer le poids du …Run Code Online (Sandbox Code Playgroud)