按“投票”数量显示帖子记录在单独的表中

发布于 2024-08-11 22:31:42 字数 255 浏览 2 评论 0原文

我是 Rails 新手,所以放轻松。我创建了一个博客,还为用户创建了表示他们“喜欢”特定帖子的功能。我实现此目的的方法是使用帖子表和单独的表“投票”。当用户单击“喜欢”按钮时,它会将记录发送到“投票”表,其中值为“1”和特定的帖子 ID。

我想在侧边栏中显示“最喜欢”的帖子。我该如何调用这样的东西呢?我想显示 post_title 和“投票”数量,也就是说,我想以某种方式查询“投票”表中记录最多的 post_id 并按降序显示它们。

我希望这是一个简单的问题。

I am new to Rails so go easy. I have created a blog and also created the ability for users to indicate they "like" a particular post. The way I am implementing this is by using the post table and a separate table 'vote'. When the user clicks the "like" button it sends the record to the 'vote' table with a value of '1' and the particular post id.

I would like to display the "most liked" posts in the sidebar. How do I go about calling such a thing. I would like to display the post_title and the number of 'votes', that is, I would like to somehow query the 'vote' table for the post_id's that have the most records and display them in descending order.

I hope this is an easy question.

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

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

发布评论

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

评论(2

幸福不弃 2024-08-18 22:31:42

有多种方法可以实现这一点,但最通用且最接近 Rails 的方法可能是创建一个带有排名方法的模块,然后让任何可以“喜欢”的类或关联扩展该模块。

# lib/likable.rb
#
module Likable
  def most_liked (limit = 10)
    # This may be possible without a find_by_sql... see the API docs.
    find_by_sql("SELECT Posts.*, SUM(votes.count) AS num_votes FROM Posts, Votes WHERE Posts.id = post_id GROUP BY post_id ORDER BY num_votes DESC LIMIT #{limit}")
  end
end

# app/models/post.rb
#
require 'likable'

class Post < ActiveRecord::Base
  extend Likable
  # ...whatever else you've got in here
end

# app/models/user.rb (or any other model with things that can be "liked")
#
require 'likable'

class User < ActiveRecord::Base
  has_many :posts, :extend => Likable
  # ...the rest of the User class
end

这可以让你做一些事情,比如......

Post.most_liked                 # => an array of the 10 most liked posts
@some_user.posts.most_liked(5)  # => that user's 5 most liked posts

如果你稍后需要,你可以向模块添加方法来查看,例如,特定帖子有多少票。您还可以将 Vote 中的 post_id 更改为 target_id 并使其成为多态关联,然后您可以使用您的 Likable 模块来投票任何东西,而不仅仅是帖子(如果您这样做,您需要概括调用以在most_liked中查找)。

There are several ways to accomplish this, but probably the most versatile and Rails-ish would be to create a module with a method to do the ranking, and then have any classes or associations that can be "liked" extend that module.

# lib/likable.rb
#
module Likable
  def most_liked (limit = 10)
    # This may be possible without a find_by_sql... see the API docs.
    find_by_sql("SELECT Posts.*, SUM(votes.count) AS num_votes FROM Posts, Votes WHERE Posts.id = post_id GROUP BY post_id ORDER BY num_votes DESC LIMIT #{limit}")
  end
end

# app/models/post.rb
#
require 'likable'

class Post < ActiveRecord::Base
  extend Likable
  # ...whatever else you've got in here
end

# app/models/user.rb (or any other model with things that can be "liked")
#
require 'likable'

class User < ActiveRecord::Base
  has_many :posts, :extend => Likable
  # ...the rest of the User class
end

This lets you do things like...

Post.most_liked                 # => an array of the 10 most liked posts
@some_user.posts.most_liked(5)  # => that user's 5 most liked posts

If you needed to later, you could add methods to the module to see, eg, how many votes a particular Post has. You could also change the post_id to a target_id in Vote and make it a polymorphic association, and then you could use your Likable module to vote for anything, not just posts (you would need to generalize the call to find in most_liked if you did that).

时常饿 2024-08-18 22:31:42

实际上,最好通过向 post 模型添加计数器缓存来完成,从而避免每次加载时都对数据库进行计数。

railscast 剧集介绍了如何设置计数器缓存。

假设您将计数器缓存命名为 votes_count,您可以执行此操作以从控制器获取 10 个最受欢迎的帖子。

@popular_posts = Post.find(:all, :limit => 10, :order => "votes_count DESC")

This is actually best done by adding a counter cache to the post model, avoiding the database count on every load.

This railscast episode explains how to setup the counter cache.

Assuming you named your counter cache votes_count, you can do this to get the 10 most popular posts from your controller.

@popular_posts = Post.find(:all, :limit => 10, :order => "votes_count DESC")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文