我有以下模型和关联:
has_many
食谱 has_many
标签 我正在尝试获取与组织的所有配方相关的所有标签。我想做类似Organization.find(1).recipes.tags
的操作,但是它不起作用。是否有一种更简单的方法来查询此内容,而不是遍历每个配方来收集其标签?
答案 0 :(得分:2)
您需要在organization_id
上使用联接和过滤器
Tag.joins(recipies: :organization).where('recipies.organization_id' => 1)
答案 1 :(得分:1)
尝试:
Tag.where(recipe: Organization.find(1).recipes)
假设为Tag belongs_to :recipe
。