使用 Acts As Votable(使用Rails 5)对多个数据库表进行投票



我一直在使用 https://github.com/ryanto/acts_as_votable gem 作为帖子的保存按钮。到目前为止,一切都很好。 👍

但是现在我创建了一个单独的脚手架(文章(并想添加相同的"保存"按钮。因此,用户可以保存帖子和文章,然后在他们的个人资料中查看。

现在我遇到了问题,因为某些文章记录与帖子记录具有相同的 id。另外,我现在如何显示已保存的记录,因为我不知道 id 来自文章或帖子。 🤔

有没有办法用可投票的宝石来解决这个问题?

谢谢! 🙏

acts_as_voteable的当前版本 (0.12.0( 开箱即用。投票模型有一个列votable_type可以引用多个模型。

#<ActsAsVotable::Vote:0x00007f9f6558a9b0
id: 4,
votable_type: "Post",
votable_id: 1,
voter_type: "User",
voter_id: 2,
vote_flag: true,
vote_scope: "save",
vote_weight: 1,
created_at: Mon, 31 Dec 2018 13:39:34 UTC +00:00,
updated_at: Mon, 31 Dec 2018 13:39:34 UTC +00:00>,
#<ActsAsVotable::Vote:0x00007f9f6558a4d8
id: 5,
votable_type: "Article",
votable_id: 3,
voter_type: "User",
voter_id: 2,
vote_flag: true,
vote_scope: "article",
vote_weight: 1,
created_at: Tue, 01 Jan 2019 15:15:27 UTC +00:00,
updated_at: Tue, 01 Jan 2019 15:15:27 UTC +00:00>

要显示保存的记录,您可以使用类似

@user.votes.for_type(Post)
@user.votes.for_type(Article)

我希望这能回答你的问题。

最新更新