0

成分.rb:

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

成分_with_quantity.rb:

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

食谱.rb:

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 回答