Created
November 29, 2010 05:47
-
-
Save datapimp/719623 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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