ฉันมี 4 โมเดลเหล่านี้เชื่อมต่อถึงกันผ่าน has_many,เป็นของสมาคม พื้นฐานมี 2 อย่าง (สูตร ส่วนผสม) อีก 2 แห่งเก็บข้อมูลเพิ่มเติมตามสองรายการก่อนหน้า
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(:ingredients).joins(:stocks) แต่ฉันได้รับข้อผิดพลาด:
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"
---
"เข้าร่วมภายใน" สุดท้ายคือฉันไม่สามารถตีความในโค้ด Rails 3 ได้ มีไอเดียอะไรบ้าง?
ขอบคุณ,