[英]Sorting microposts by number of comments (Rails)?
I have 2 models: Microposts (which has_many :comments
) and Comments (which belongs_to :micropost
). 我有2个模型: Microposts (具有
has_many :comments
)和Comments ( belongs_to :micropost
)。
In the following view: 在以下视图中:
views/micropost/index.html.erb: views / micropost / index.html.erb:
<h2>Micropost Index</h2>
<% @microposts.each do |micropost| %>
<h2><%= micropost.title %></h2>
<p><%= micropost.content %></p>
<p><%= micropost.comments.count %></p>
<ul>
<li><%= link_to 'Show', micropost %></li>
<li><%= link_to 'Edit', edit_micropost_path(micropost) %></li>
<li><%= link_to 'Destroy', micropost, confirm: 'Are you sure?', method: :delete %></li>
</ul>
<br />
<% end %>
controller/microposts.rb: 控制器/microposts.rb:
def index
@microposts = Micropost.all
end
I was able to get the number of comments for each micropost: 我能够获得每个微博的评论数:
<p><%= micropost.comments.count %></p>
How can I sort microposts by number of comments? 如何按评论数对微博进行排序?
Any suggestions to accomplish this? 有什么建议可以做到这一点?
You can do a dumb sort by counting comments for each micropost, but that is not a good idea. 您可以通过对每个微博的评论进行计数来进行愚蠢的排序,但这不是一个好主意。 Cause every time your index action is invoked, rails will perform N+1 database queries, where N is the quantity of microposts.
因为每次您的索引动作被调用时,rails都会执行N + 1个数据库查询,其中N是微博的数量。
The best solution here is to use Rails counter_cache feature. 最好的解决方案是使用Rails counter_cache功能。 It's a separate column in your microposts database table, where comments quantity is stored.
它是您的微博数据库表中的单独一列,用于存储评论数量。 Also, it updates automatically every time you create new or delete a comment.
另外,每次您创建新评论或删除评论时,它都会自动更新。
First, you need to add a column to your database. 首先,您需要在数据库中添加一列。 To do this, create a migration with something like this:
add_column :microposts, :comments_count, :integer, :default => 0, :null => false
为此,请使用以下内容创建迁移:
add_column :microposts, :comments_count, :integer, :default => 0, :null => false
After that change the code in your comment model to something like that: belongs_to :micropost, :counter_cache => true
之后,将您的注释模型中的代码更改为类似的内容:
belongs_to :micropost, :counter_cache => true
After all these changes your comments_count column in micropost will increment every time you create new comment and decrement every time you delete a comment. 完成所有这些更改后,每次创建新评论时,micropost中的comment_count列都会增加,而每次删除评论时都会减少。 Also, you can sort microposts by this column's value as well like this:
另外,您可以按此列的值对微博进行排序,如下所示:
@microposts = Micropost.order('comments_count ASC').all
You can create a method in micropost.rb
and sort by it like a virtual attribute: 您可以在
micropost.rb
创建一个方法,并像虚拟属性一样对其进行排序:
def comments_count
comments.count
end
Then sort your collection with sort_by
method: 然后使用
sort_by
方法对集合进行排序:
<% @microposts.sort_by(&:comments_count).map do |micropost| %>
<%= micropost.title %>
...
<% end %>
Also, you can use reverse
method if you need an opposite sort direction. 另外,如果需要相反的排序方向,则可以使用
reverse
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.