2
私はこれらの4つのモデルをhas_many、belongs_toアソシエーションで相互に接続しています。基本は2(Recipe、Ingredient)です。他の2つは、前の2つに基づいて追加情報を格納します。レール3 ActiveRecordアソシエーション
class Recipe
has_many :ingredients, :through => :prescriptions
has_many :prescriptions
end
class Ingredient
has_many :recipes, :through => :prescriptions
has_many :prescriptions
has_many :stocks
end
class Stock
belongs_to :ingredient
end
class Prescription
belongs_to :recipe
belongs_to :ingredient
end
私はRecipe.joinsで、利用可能在庫されているレシピをフェッチしよう(:成分).joins(:株式)しかし、私はエラーを取得:
ActiveRecord::ConfigurationError: Association named 'stock' was not found; perhaps you misspelled it?
SQLクエリ私は、で到達しよう:
SELECT "recipes".* FROM "recipes"
INNER JOIN "prescriptions"
ON "prescriptions"."recipe_id" = "recipes"."id"
INNER JOIN "ingredients"
ON "ingredients"."id" = "prescriptions"."ingredient_id"
---
INNER JOIN "stocks"
ON "stocks"."ingredient_id" = "ingredients"."id"
---
最後の「INNER JOINはすることは」私はレール3のコードで解釈することができないということです。何か案は?
は
オリジナルモデルをオフに参加していないということを伝える必要があり、ありがとう!これは正しいです!私はそれが簡単だとは信じられない!ありがとうございました! –