Kevin K
Kevin K

Reputation: 2227

Rails query in controller with multiple conditions

I have the following relationships set up in my models for Festivals, Categories, Submissions and Curators (users).

When a curator (a user) is logged in and they look at the index of submissions. They should only see submissions for which they are curator (through category). I'm stumped on how to generate the proper list of submissions. Here is what is in the controller now:

def index
  @submissions = current_festival.submissions.all
end

This would return all the submissions for the current festival, not just from the categories that the current_user is the curator. What I want is something like this, but I don't know the proper syntax:

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

Any ideas what the proper syntax would be?

Upvotes: 0

Views: 249

Answers (1)

LHH
LHH

Reputation: 3323

This will give you all the submission that belongs to category which are created by 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

Upvotes: 2

Related Questions