0

我在我的模型中为节日、类别、提交和策展人(用户)设置了以下关系。

  • 节日有很多类别。每个类别都有一个节日。
  • 每个类别都有一个或多个策展人(一个用户),并且策展人可以有多个类别。
  • 提交的作品有一个类别和一个节日(通过它的类别)。
  • “current_user”是登录的用户
  • “current_festival”是目前正在接受新提交的一个节日

当策展人(用户)登录并查看提交索引时。他们应该只看到他们是策展人的提交(通过类别)。我对如何生成正确的提交列表感到困惑。这是控制器中的内容:

def index
  @submissions = current_festival.submissions.all
end

这将返回当前节日的所有提交,而不仅仅是来自 current_user 是策展人的类别。我想要的是这样的,但我不知道正确的语法:

def index
  @categories = current_user.categories.where(festival_id: current_festival.id)
  @submissions = current_festival.submissions.where( category_id: "one of the @categories" )
end

任何想法正确的语法是什么?

4

1 回答 1

2

这将为您提供属于 current_user 创建的类别的所有提交

def index
  category_ids = current_user.categories.where(festival_id: current_festival.id).collect(&:id)
  @submissions = current_festival.submissions.where(category_id: category_ids)
end
于 2013-10-30T21:58:04.423 回答