[英]Rails 3: How to make a join in mysql over 3 tables (with :through in the model)
ingredient.rb:
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:
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中查找此查询?
谢谢你的协助!!
它是:ingredient_with_quantities
而不是:ingredients_with_quantities
(你在ingredient
之后加上“s”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.