Rails 3 在 mongoid 嵌入文档上使用 kaminari 进行分页

发布于 2024-11-25 04:56:25 字数 276 浏览 2 评论 0原文

当我在嵌入文档集合上使用 kaminari 调用 paginate 时,出现以下错误:

(Access to the collection for Document is not allowed since it is an embedded document, please access a collection from the root document.):

知道如何解决这个问题吗?我已经将 kaminari 作为 gem 安装了。

亚历克斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昔日梦未散 2024-12-02 04:56:25

您只需要通过父对象访问集合即可。例如,给定以下模型:

class User
  include Mongoid::Document
  embeds_many :bookmarks
end

class Bookmark
  include Mongoid::Document
  embedded_in :user
end

然后要对给定用户的书签进行分页,您可以执行以下操作:

@user.bookmarks.page(params[:page])

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])
分分钟 2024-12-02 04:56:25

我在 Kaminari 上发现了这个问题: https://github.com/amatsuda/kaminari/issues/89< /a>

所以我分叉了它,并按照 spatrik 提供的解决方案修复了它。我不能 100% 确定它适用于所有情况,并且该解决方案没有任何缺点。但目前它的工作原理与预期完全一致。

亚历克斯

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

也只是曾经 2024-12-02 04:56:25

我刚刚针对该问题发送了一个补丁。看看请求。希望这有助于解决您的问题。
https://github.com/amatsuda/kaminari/pull/155/files

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

甜是你 2024-12-02 04:56:25

对于前面的 theTRON 示例:

class User
  include Mongoid::Document
  embeds_many :bookmarks
end

class Bookmark
  include Mongoid::Document
  field :created_at, :type => DateTime

  embedded_in :user
end

下面的示例将给您带来您在帖子中描述的错误:

@user.bookmarks.desc(:created_at).page(params[:page])

而下一个示例将正常工作:

@user.bookmarks.page(params[:page]).desc(:created_at)

我希望它有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文