内容投票数据库和应用程序设计

发布于 2024-07-08 21:00:54 字数 1454 浏览 7 评论 0原文

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

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

发布评论

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

评论(3

静水深流 2024-07-15 21:00:54

我建议从“可投票”接口的单表继承开始,并从那里派生任何可投票类。
STI 的起始详细信息:http://wiki.rubyonrails.org/rails/pages/singletableinheritance

I'd suggest starting with Single Table Inheritance for a 'votable' interface and derive any votable classes from there.
Starting details for STI: http://wiki.rubyonrails.org/rails/pages/singletableinheritance

两人的回忆 2024-07-15 21:00:54

你说多态——这是关键词。

看看 ActsAsTaggable 是如何工作的。 您可以创建一个 ActsAsVotable“插件”,为特定模型提供投票行为。 投票将存储在另一个模型(投票?)中,该模型将具有多态belongs_to。

create_table :orders do |t|
  t.string :votable_type
  t.integer :votable_id
  t.integer :vote
  t.timestamps
end

您的 acts_as_votable 调用将在幕后将其与其投票关联起来:

has_many :votes, :as => "votable"

再次参见 ActsAsTaggable。 :)

You said polymorphically -- that's the key word.

Look at how ActsAsTaggable works. You can create an ActsAsVotable "plugin" that will provide voting behavior to specific models. Votes will be stored in another model (Vote?) that will have a polymorphic belongs_to.

create_table :orders do |t|
  t.string :votable_type
  t.integer :votable_id
  t.integer :vote
  t.timestamps
end

Your acts_as_votable call will behind the scenes associate it with its votes:

has_many :votes, :as => "votable"

Again, see ActsAsTaggable. :)

当爱已成负担 2024-07-15 21:00:54

谢谢你们的回答。 是的,创建多态投票模型是可行的方法,但是我试图提取更完整的答案。

例如,这就是我到目前为止的想法。 在这种情况下,可投票类可以与多个类(文章、问题、产品)绑定。

投票表:id:integer、vote:boolean、voteable_type:string(被投票类别的名称)、voteable_id:integer(被投票类别的 id)、voter_id(用户投票的标识符)。

现在我们仍然需要尝试将投票限制为每个用户一张,而不需要某种注册。 因此 voter_id 可以是包含用户 IP 地址和 user_agent 的组合。 这就是我乐于接受想法的地方。 如果捕获的 IP 地址来自公司代理等,则它们不一定是唯一的,因此结合用户代理应该会有所帮助。 但是,仍然存在两个用户可以具有相同 IP 和用户代理字符串的情况。 此外,如果浏览器未在请求标头中发送此信息,您可能并不总是能够获取此信息,这意味着某些用户根本无法投票……此外,IP 可能会被欺骗。

另一种可能性是使用 cookie,但我认为这会招致用户的滥用,这些用户希望通过清除 cookie 或脚本点击来增加对特定内容的投票。 在此处检查用户代理也应该有所帮助,但是您可以通过脚本轻松伪造用户代理。

可能没有完美的解决方案,但我很好奇其他人是如何解决这个问题的。 即黑客新闻甚至 stackoverflow。

Thanks for the answers guys. Yup, creating a polymorphic votable model is the way to go, however I was attempting to extract a more complete answer.

For example here's what I'm thinking so far. A voteable class which can be tied to multiple classes (Article, Question, Product) in this scenario.

Votes table: id:integer, vote:boolean, voteable_type:string (name of the class being voted on), voteable_id:integer (id of the class being voted on), voter_id (identifer for the user voting).

Now we still need to try and limit votes to one per user without requiring some sort of registration. So the voter_id could be a composite containing the user's IP address and user_agent. This is where I'm open to ideas. The IP address captured is not necessarily unique if they're coming from a corp proxy etc. so combining the user agent shoud help somewhat. However you still have cases where two user's can have the same IP and user agent string. Also, you may not always be able to obtain this information if their browser doesn't send it in the request header, meaning that some user's simply won't get to vote... Also, IPs can be spoofed.

Another possiblity would be using cookies, but I think this invites abuse from user's who want to spike the votes for a particular piece of content by clearing their cookies or scripting hits. Checking the user-agent here should help too, but you can fake a user-agent via script easily enough.

There probably is no perfect solution, but I'm curious how other's have approached this. I.e. hacker news and even stackoverflow.

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