Search code examples
ruby-on-rails-3modelcontrollerscopebelongs-to

Only show submissions for the current festival


I have models in my application for Festivals and Submissions. Each submission belongs to a single festival. And I have a helper to determine what is the "current_festival" based on dates for each festival.

module ApplicationHelper

  def current_festival
    @current_festival ||= Festival.current.first
  end

end

.current is a scope in the festival model:

  scope :current, where("published = true and closing_date >= ?", Time.now.to_date).order('closing_date asc').limit(1)

What I'd like is to restrict the submissions shown in the index view to ones that belong to the current festival only. How would you do that in the controller? Or would it be best to somehow do that in the model, perhaps through a scope?


Solution

  • I suppose you have a relation defined like this:

    class Festival
      has_many :submissions
    end
    

    Then you can do in any place:

    Festival.current.submissions