0

成分.rb:

class Ingredient < ActiveRecord::Base
  has_many :recipes, :through => :ingredients_with_quantities
  has_many :ingredients_with_quantities

成分の量.rb:

class IngredientWithQuantity < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient

レシピ.r​​b:

class Recipe < ActiveRecord::Base
  has_many :ingredients, :through => :ingredients_with_quantities
  has_many :ingredients_with_quantities

特定の材料名を含むすべてのレシピを取得するクエリを作成したいと思います。

このクエリを試しました:

Recipe.find(:all, :include => {:ingredients_with_quantities => :ingredients}, :conditions => "ingredients.name = 'ingredient1'")

しかし、私はこのエラーが発生します:

NameError: uninitialized constant Recipe::IngredientsWithQuantity

誰かがクエリの何が問題なのか教えてもらえますか?

次のコマンドを使用して、SQL でクエリを成功させることができます。

SELECT r . * FROM recipes r, ingredient_with_quantities iq, ingredients i 
WHERE i.name =  "ingredient1"
AND iq.recipe_id = r.id 
AND iq.ingredient_id = i.id

ActiveRecord を使用した Rails では、このクエリはどのように見えますか?

助けてくれてありがとう!!

4

1 に答える 1

2

では:ingredient_with_quantitiesなく:ingredients_with_quantities(の後に「s」を付けますingredient

于 2012-04-30T15:17:29.730 に答える