I need to calculate what page a comment is on so I can create a direct link to it. I'm using kaminari pagination with monogid. On kaminari's wiki they have a solution on how to do it with activerecord but I'm not sure the best way to translate that into mongoid.
Asked
Active
Viewed 138 times
2 Answers
0
If you are using Mongoid 3, this method does the job:
class User
include Mongoid::Document
...
def page_num(options = {})
field = options[:by] || :_id
order = options[:order] || :asc
per = options[:per] || self.class.default_per_page
operator = (order == :asc) ? field.to_sym.lte : field.to_sym.gte
(self.class.where(operator => read_attribute(field)).order_by("#{field} #{order}").count.to_f / per).ceil
end
...
end
Hope this helps.

Yuki Nishijima
- 1,802
- 2
- 16
- 19
0
Here is a stripped down version that I ended up coming up with.
def page_num
number_before = self.class.where(recipe_id: recipe.id).gte(created_at: created_at).count.to_f
number_on_page = self.class.default_per_page
(number_before / number_on_page).ceil
end

hadees
- 1,754
- 2
- 25
- 36