Skip to content

Instantly share code, notes, and snippets.

@datapimp
Created November 29, 2010 05:47
Show Gist options
  • Save datapimp/719623 to your computer and use it in GitHub Desktop.
Save datapimp/719623 to your computer and use it in GitHub Desktop.
class Book < ActiveRecord::Base
scope :category, lambda {|cat| where(:category_id => cat) }
scope :author, lambda {|auth| where(:authori_id => auth) }
# example of a method to build chainable scope
# based on the query parameters
def self.query params={}
scoped = limit(25)
scoped = scoped.category( params[:category_id] ) if params[:category_id].present?
scoped = scoped.author( params[:author_id] ) if params[:author_id].present?
scoped
end
end
class BooksController < ApplicationController
def index
@books = Book.query(params)
if request.xhr?
# renders just a list of records
# for use by jquery
render :partial => "books/listing"
else
# renders full template, with layout
render
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment