Will_Paginate

Hi Guys, In my application I created this method:

def self.find_paginated_posts_by_post_type(post_type,page=1,query=“”) if query.empty? == false query = “%#{query}%” else query = “%” end result = includes(:user). where([‘post_type_id = ? AND title LIKE ?’, post_type.id, query]). order(‘created_at DESC’). page(page) return result end

this method is returning an ActiveRecord::Relation Object. So, when I use will_paginate method my application throws an exception :

undefined method `total_pages' for #<ActiveRecord::Relation:0x007f94581e7848>

How can I fix it? Thx

Bruno Meira

Hi Guys, In my application I created this method:

  def self.find_paginated_posts_by_post_type(post_type,page=1,query="")     if query.empty? == false       query = "%#{query}%"     else       query = "%"     end     result = includes(:user).              where(['post_type_id = ? AND title LIKE ?', post_type.id, query]).              order('created_at DESC').              page(page)    return result   end    this method is returning an ActiveRecord::Relation Object. So, when I use will_paginate method my application throws an exception :

undefined method `total_pages' for #<ActiveRecord::Relation:0x007f94581e7848>

How can I fix it? Thx

If you add to_a to the end of the request, before you pass it to paginate, then all of the results will be queried and returned as an array -- old-school. But if you use Kaminari or another pagination system that works natively with ActiveRecord::Relation objects, you won't have this issue (and you will get the benefits of Arel and all the wonder that is Rails 3+).

Walter

Hi Walter, I will take a look in this solution and in this Kamirami Thx for the answer ;D

First time using Kaminari… But it seems to be a good gem. Thx for the tip ;D