我有这种关系
class Cupboard
include Mongoid::Document
field :name, type: String
has_many :ingredients
end
class Recipe
include Mongoid::Document
field :name, type: String
hes_many :ingredients
end
class Ingredient
include Mongoid::Document
field :name, type: String
field :description, type: String
belongs_to :cupboard
belongs_to :recipe
end
我需要在Cupboard模型中创建一个方法来查找包含与橱柜相同成分的配方,我在Mongoid文档中找不到找到它的方法。
我需要像Recipe.find( #all cupboard.ingredients.ids )
提前致谢
答案 0 :(得分:1)
def shared_recipes
ingredients.map(&:recipe).uniq
end