新手问题 新手问 activerecord-reputation-system 的问题

xiaoniuniu · 2013年09月12日 · 最后由 xiaoniuniu 回复于 2013年09月12日 · 1887 次阅读

学习了 reailcast 上的课程 364。 可以在网站上实现 like 的功能,不过有一个问题 目前网站上有 product,user 可以点是不是喜欢这个 product。问题在于如何从 user 页读取这个人点了几个喜欢的 product 呢? 我现在的 product 和 user 之间没有任何关系。 我在 user.rb 中加了

has_reputation :votes, source: {reputation: :votes, of: :haikus}, aggregated_by: :sum

在 user 的 show.html.erb 中加了

<%= @user.reputation_for(:votes).to_i %>

但最后显示的数字都是 0,也就是当时 user 点击的没有被记录。。。这是为什么呢?

  1. haikus 是该 railscast 里的示例 model,对应你的应用里就是 products。因此就像视频里讲的那样,你的 userproduct 应该有关系,而不是没有任何关系;在原视频里,haiku belongs_to useruser has_many haikus

  2. 所以你在你的 user.rb 里加的那一句明显是错误的:

# Wrong source
has_reputation :votes, source: { reputation: :votes, of: :haikus }, aggregated_by: :sum

# Correct source
has_reputation :votes, source: { reputation: :votes, of: :products }, aggregated_by: :sum

#1 楼 @nightire 对的对的,我代码里面写得是 products,抱歉打错了 我发现我的 product 和 user 没有关系的,可是我不知道怎么解决~实际操作就是他们之间没有关系呀。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号