On Feb 9, 2012, at 5:11 PM, Bruno Meira wrote:
> 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
>
> Bruno Meira
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.