2012-04-30 13 views
0

ingredient.rb:3つのテーブルからMySQLに参加するようにする方法レール3:(:モデルにスルー付き)

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

ingredient_with_quantity.rb:

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

recipe.rb:

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

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

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の中に見えていますか?

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

答えて

2

それは:ingredient_with_quantitiesはなく:ingredients_with_quantities(あなたがingredient後に "s" を置く)

+0

OMGである...私はそれを見ていません!ご助力ありがとうございます! –

+0

モデルを "has_many:ingredient_with_quantities"に変更し、クエリを: Recipe.find(:all、:include => {:ingredients_with_quantities =>:ingredients}、:conditions => "ingredients.name = 'ingredients1'")に変更しました。 –

+0

問題は解決しますか? 2 ou 3のレールを使用していますか? –