rails 3 pagination with kaminari on mongoid embedded documents
When I call paginate with kaminari on a collection of embedded documents I get the following error:
(Access to the collection for Document is not allowed since it is an embedded document, please access a collection from the root document.):
Any idea on how I can开发者_开发问答 fix that ? I have installed kaminari as a gem.
Alex
You just need to access the collection through the parent object. For example, given the following models:
class User
include Mongoid::Document
embeds_many :bookmarks
end
class Bookmark
include Mongoid::Document
embedded_in :user
end
Then to paginate a given user's bookmarks you would do:
@user.bookmarks.page(params[:page])
I found this issue on Kaminari: https://github.com/amatsuda/kaminari/issues/89
So I forked it, and fixed it following the solution provided by spatrik. I am not 100% sure it will work on all cases and that this solution does not have any drawbacks. But for the moment it works exactly as expected.
Alex
I just sent a patch for the issue. Take a look at the request. hope this helps solve your issue.
https://github.com/amatsuda/kaminari/pull/155/files
With the previous theTRON example :
class User
include Mongoid::Document
embeds_many :bookmarks
end
class Bookmark
include Mongoid::Document
field :created_at, :type => DateTime
embedded_in :user
end
the following, will get you the error you described in your post :
@user.bookmarks.desc(:created_at).page(params[:page])
while the nex one will works fine :
@user.bookmarks.page(params[:page]).desc(:created_at)
I hope it help.
精彩评论