我需要通过关联模型查询我的模型。通过关联查询模型有很多通过关联
伪代码: @drinks = Drink.where(drink.ingredients在cabinet.ingredients)
饮料模型
class Drink < ActiveRecord::Base
attr_accessible :name
has_many :recipe_steps, :dependent => :destroy
has_many :ingredients, through: :recipe_steps
end
配料模型
class Ingredient < ActiveRecord::Base
attr_accessible :name
has_many :recipe_steps
has_many :drinks, through: :recipe_steps
has_many :cabinet_ingredients
belongs_to :cabinet
end
用户模型
class User < ActiveRecord::Base
has_one :cabinet
end
编辑:建议我尝试
但是它返回同饮的多个记录时,我有一个饮料与我的内阁和/或多个用户2种成分。
我需要回到只有一个饮料的记录。此外,我只需要返回的饮料,如果它的所有成分在内阁
关于下面的答案:如果你想消除重复记录,你可以在你的'@ drinks'数组上调用'.uniq'。 – 2013-02-23 00:09:51
好的,但我如何缩小到特定用户? – BrianJakovich 2013-02-23 01:35:05