ราง 3 การเชื่อมโยง ActiveRecord

ฉันมี 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 ได้ มีไอเดียอะไรบ้าง?

ขอบคุณ,


person Kostas    schedule 25.10.2011    source แหล่งที่มา


คำตอบ (1)


ไม่มีโมเดลให้ตรวจสอบ แต่นอกใจ ต้องบอกว่าไม่ได้ร่วมจากโมเดลดั้งเดิม

Recipe.joins(:ingredients).joins(:ingredients => :stocks).select:
person John Hinnegan    schedule 25.10.2011
comment
ใช่! นี่ถูกต้อง! ฉันไม่อยากจะเชื่อเลยว่ามันง่ายขนาดนั้น! ขอบคุณ! - person Kostas; 26.10.2011