Skip to content

Instantly share code, notes, and snippets.

@barek2k2
Created June 9, 2014 19:40
Show Gist options
  • Save barek2k2/09f296581eb0fc727cfe to your computer and use it in GitHub Desktop.
Save barek2k2/09f296581eb0fc727cfe to your computer and use it in GitHub Desktop.
Rails Model Controller Example
class Course < ActiveRecord::Base
def self.search(params)
courses = []
if params[:subject_id].present?
subject = Subject.find(params[:subject_id])
courses = subject.courses
elsif params[:near].present?
courses = Course.around_me
elsif params[:most_popular].present?
courses = Course.most_popular
elsif params[:top_rated_place].present?
courses = Course.select("COUNT(*) AS total,courses.*").order("total DESC").group(:city)
elsif params[:type].present?
courses = Course.with_type(params[:type])
elsif params[:this_week].present?
courses = Course.this_week
elsif params[:this_month].present?
courses = Course.this_month
elsif params[:upcoming].present?
courses = Course.upcoming
else
courses = Course.sphinx_search(params)
end
courses
end
end
class CoursesController < ApplicationController
def index
@courses = Course.search(params).paginate :per_page => Course::PER_PAGE, :page => params[:page]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment