Ruby on Rails will_pagination 一个数组
我想知道是否有人可以解释如何在对象数组上使用 will_paginate ?
例如,在我的网站上,我有一个意见部分,用户可以在其中对意见进行评分。这是我写的一个方法来收集对意见进行评分的用户:
def agree_list
list = OpinionRating.find_all_by_opinion_id(params[:id])
@agree_list = []
list.each do |r|
user = Profile.find(r.profile_id)
@agree_list << user
end
end
谢谢
I was wondering if someone could explain how to use will_paginate on an array of objects?
For example, on my site I have an opinion section where users can rate the opinions. Here's a method I wrote to gather the users who have rated the opinion:
def agree_list
list = OpinionRating.find_all_by_opinion_id(params[:id])
@agree_list = []
list.each do |r|
user = Profile.find(r.profile_id)
@agree_list << user
end
end
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
will_paginate 3.0 旨在利用 Rails 中新的
ActiveRecord::Relation
3,因此默认情况下它仅在关系上定义分页。它仍然可以与数组一起使用,但您必须告诉 Rails 需要该部分。在您的
config/initializers
中的文件中(我使用了will_paginate_array_fix.rb
),添加此内容然后您可以在数组上使用
will_paginate 3.0 is designed to take advantage of the new
ActiveRecord::Relation
in Rails 3, so it definespaginate
only on relations by default. It can still work with an array, but you have to tell rails to require that part.In a file in your
config/initializers
(I usedwill_paginate_array_fix.rb
), add thisThen you can use on arrays
您可以使用 Array#from 来模拟分页,但真正的问题是您根本不应该使用 Array。
这就是 ActiveRecord 关联 的用途。您应该仔细阅读该指南,如果您正在开发 Rails 应用程序,则需要了解很多有用的内容。
让我向您展示一种更好的方法来完成同样的事情:
重要的是您的数据库模式遵循正确的命名约定,否则这一切都会被破坏。确保您使用数据库迁移创建表,而不是这样做用手。
这些关联将在您的模型上创建助手,使搜索变得更加容易。您可以使用
named_scope
或scope
让 Rails 为您执行此操作,而不是迭代 OpinionRatings 列表并手动收集用户,具体取决于您是否使用 Rails 2.3 或 3.0。既然你没有具体说明,我就举两个例子。将其添加到您的 OpinionRating 类中:2.3
3.0
无论哪种情况,您都可以在
OpinionRatings
上调用for(id)
model 并传递给它一个 id:2.3
3.0
所有这一切的结果是你现在可以轻松分页:
Rails 4.x 的更新:或多或少相同:
虽然对于较新的 Rails,我偏好使用 kaminari 进行分页:
You could use
Array#from
to simulate pagination, but the real problem here is that you shouldn't be usingArray
at all.This is what ActiveRecord Associations are made for. You should read that guide carefully, there is a lot of useful stuff you will need to know if you're developing Rails applications.
Let me show you a better way of doing the same thing:
It's important that your database schema is following the proper naming conventions or all this will break. Make sure you're creating your tables with Database Migrations instead of doing it by hand.
These associations will create helpers on your models to make searching much easier. Instead of iterating a list of OpinionRatings and collecting the users manually, you can make Rails do this for you with the use of
named_scope
orscope
depending on whether you're using Rails 2.3 or 3.0. Since you didn't specify, I'll give both examples. Add this to your OpinionRating class:2.3
3.0
In either case you can call
for(id)
on theOpinionRatings
model and pass it an id:2.3
3.0
The upshot of all this is that you can now easily paginate:
Update for Rails 4.x: more or less the same:
Although for newer Rails my preference is kaminari for pagination:
gem
will_paginate
将对 ActiveRecord 查询和数组进行分页。The gem
will_paginate
will paginate both ActiveRecord queries and arrays.如果您不想使用配置文件或遇到问题,您也可以确保返回 ActiveRecord::Relation 而不是数组。例如,将同意列表更改为用户 ID 列表,然后对这些 ID 执行 IN 以返回关系。
从数据库的角度来看,这是低效的,但对于任何对 will_paginate 配置文件有问题的人来说,这是一个选项。
If you don't want to use the config file or are having trouble with it, you can also just ensure you return an ActiveRecord::Relation instead of an array. For instance, change the agree_list to be a list of user ids instead, then do an IN on those ids to return a Relation.
This is inefficient from a database perspective, but it's an option for anybody having issues with the will_paginate config file.
我利用了 Rails 协会的优势,想出了一个新方法:
在我看来,我像这样查找了个人资料:
如果有更好的方法,请发布。我尝试在 OpinionRating 模型中使用named_scope 帮助器,但没有成功。这是我尝试过但不起作用的示例:
但这看起来与使用 find 方法相同。
感谢您的所有帮助。
I took advantage of rails associations, and came up with a new method:
In my view I looked up the profile like so:
Please post up if there's a better way to do this. I tried to use the named_scope helper in the OpinionRating model with no luck. Here's an example of what I tried, but doesn't work:
That seemed like the same as using the find method though.
Thanks for all the help.
我正在使用 Rails 3 ruby 1.9.2。另外,我刚刚启动应用程序,所以不包含 css 或样式。
安装 will_paginate:
gem install will_paginate
添加到 Gemfile 并运行包。
Controller
模块 StructHelper 查询服务,而不是数据库。
structHelperGet() 返回一个记录数组。
不确定更复杂的解决方案是否是伪造模型,或者经常获取数据并偶尔重新创建 sqllite 表并有一个真实的模型可供查询。刚刚创建我的第一个 Rails 应用程序。
查看
这将为您提供每页默认 30 行的分页。
如果您还没有阅读 http://railstutorial.org,请立即开始阅读。
I am using rails 3 ruby 1.9.2. Also, I am just starting app, so no css or styles included.
Install will_paginate:
gem install will_paginate
Add to Gemfile and run bundle.
Controller
module StructHelper queries a service, not a database.
structHelperGet() returns an array of records.
Not sure if a more sophisticated solution would be to fake a model, or to grab the data every so often and recreate a sqllite table once in a while and have a real model to query. Just creating my first rails app ever.
View
This will give you pagnation of the default 30 rows per page.
If you have not read http://railstutorial.org yet, start reading it now.
即使没有任何 gem,你也可以实现分页。我看到了这个 如何对数组进行分页?。 kaminari gems 文档中的简单实现。请参阅我从 kaminari gems 文档获得的以下示例
You can implement pagination even without any gem.I saw this How do I paginate an Array?. Simple implementation in kaminari gems doc. Please see the below example which i got from kaminari gems doc