0

每次用户单击 link_to 时,方法都应更新属性。累积belongs_to :user 和:question

 <%= link_to "+1", controller: "/accumulations", action: "vote_up",id: @question, user_id: current_user, method: "post" %>

def vote_up
 @question = Question.find(params[:id])
 @accumulation = Accumulation.where(user_id: current_user.id, question_id: @question.id )

累积的第三个属性是点:

 @accumulation.point = Accumulation.increment_counter(:point, @accumulation)
 @accumulation.save
 redirect_to :back, notice: "+1"
end

它给了我这个信息:

#ActiveRecord::Relation:0xaa7fc10 的未定义方法 `point='

如何更新属性?

4

1 回答 1

0

假设任何给定的用户/问题组合只有一个累积,您可以添加.first到您的查询中:

@accumulation = Accumulation.where(user_id: current_user.id, question_id: @question.id ).first

否则,您将尝试设置point记录集合而不是单个 ActiveRecord 对象。

于 2013-08-13T13:23:01.557 回答