理解将对范围/关系进行分页
为什么 Post.page(1).total_pages 结果是:
Post.page(1).total_pages
undefined local variable or method `total_pages' for #<ActiveRecord::Relation:0x00000006a95230>
但
Post.scoped.page(1).total_pages
工作正常。奇怪的是,
Post.paginate(:page => 1).total_pages
效果很好。我查看了 Github 上的代码( https://github.com /mislav/will_paginate/blob/master/lib/will_paginate/active_record.rb ),我可以明白为什么分页起作用(因为它首先调用 limit ...它返回一个活动记录关系,就像作用域一样)。我有一种感觉,它与这段代码有关
rel = scoped.extending(RelationMethods)
,我想我不明白这些限制返回的活动记录关系与scoped.extending(RelationMethods)之间的区别。有什么想法吗?
Why does Post.page(1).total_pages result in:
Post.page(1).total_pages
undefined local variable or method `total_pages' for #<ActiveRecord::Relation:0x00000006a95230>
but
Post.scoped.page(1).total_pages
works fine. Curiously,
Post.paginate(:page => 1).total_pages
works fine. I looked at the code on Github ( https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/active_record.rb ) and I can see why paginate works (because it calls limit first... which returns an active record relation, much like scoped does). I have a feeling it has something to do with this code
rel = scoped.extending(RelationMethods)
I guess I don't understand the difference between these the active record relation that limit returns versus scoped.extending(RelationMethods). Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当使用rails_admin gem 时才会发生这种情况。它在带有 will_paginate 3.0.2 的新 Rails 3.1.1 应用程序中运行良好。
Rails 管理员可能正在对页面方法执行某些操作,但我不确定具体是什么。
This only happens when using the rails_admin gem. It works fine in a fresh Rails 3.1.1 app with will_paginate 3.0.2.
Rails admin is probably doing something to the page method, though I'm not sure what exactly.