2
我有這4個模型通過has_many,belongs_to相互關聯。基本是2(食譜,配料)。另外2個商店基於前兩個存儲額外的信息。rails 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」是我無法在rails 3代碼中解釋。有任何想法嗎?
謝謝
是!這是對的!我簡直不敢相信那麼簡單!謝謝! –