Search code examples
paginationruby-on-rails-3.2kaminari

Kaminari pagination with conditions on rails


i am trying to add conditions to a Kaminari pagination on my rails project and i am finding it almost impossible. here is my code

  UserController
#feed=Feed.order('created_at desc').page(params[:page], :conditions=>["source=? or        source=?","beep","sub_beep"])

the above didn't work soo i tried the following

  @feed1 = Feed.find(:all,:conditions=>["source=? or source=?","beep","sub_beep"])
  @feed= Kaminari.paginate_array(@feed1).page(params[:page]).per(2)

the above displays contents but it doesnt display 2 contents per page and the displayed elements arent ordered.

i need a way to add conditions to kaminari pagination and order the result

please i need help!!! :(


Solution

  • Kaminari is designed to work with ActiveRecord 3+ style query interface only.

    Please avoid using :conditions "Hasheritis" syntax which will be deprecated sooner or later, but chain with where method instead.

    Usage of the where method is well documented here on the Rails Guides: http://guides.rubyonrails.org/active_record_querying.html#conditions

    Your controller code should be like this:

    feeds = Feed.where(:source => ['beep', 'sub_beep']).order('created_at desc').page(params[:page])