Skip to content

Instantly share code, notes, and snippets.

@alnutile
Created June 7, 2013 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alnutile/5726820 to your computer and use it in GitHub Desktop.
Save alnutile/5726820 to your computer and use it in GitHub Desktop.
Model and Controller after moving the query logic into the model and using scopes
@speaker_requests_upcoming = SpeakerRequest.published.content(search).date_range(date_start, date_end)
@speaker_requests_upcoming = @speaker_requests_upcoming.paginate(page: params[:page], :order => "date ASC")
scope :published, -> { where published: true }
scope :content, ->(searched) { where(["content LIKE ?", "%#{searched}%"]) }
scope :date_range, ->(start_date, end_date) {
where("date between ? and ?", start_date, end_date)
}
@adkron
Copy link

adkron commented Jun 7, 2013

Not bad. Don't forget that you can add order as a scope also.

scope :by_date, -> { order(:date) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment