每次用户单击link_to时,方法都应该更新属性。累计belongs_to:用户和:问题
<%= 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="
如何更新属性?
假设任何给定的用户/问题组合只有一个累积,则可以将.first
添加到查询中:
@accumulation = Accumulation.where(user_id: current_user.id, question_id: @question.id ).first
否则,您将尝试在记录集合而不是单个ActiveRecord对象上设置point
。