2011-08-25 45 views
0

我正在构建一个简单的排名系统。当评论被投票时,用户的rank得到提升。 @comment.rating得到更新并保存,因为它应该。但是,@comment.user.rank没有。它不报告任何错误,它从不尝试更新或保存它。排名系统没有更新db记录

这是我的意见控制器的方法:

def increment 
    @comment = Comment.find(params[:id]) 
    @comment.rating += 1 
    @comment.user.rank += User::RANK_VALUES["comment_plus"] 
    if @comment.save 
    flash[:notice] = "Voted up" 
    redirect_to(:back) 
    else 
    flash[:notice] = "Error" 
    redirect_to(:back) 
    end 
end 

这就是所谓的与此link_to

<%= link_to "+", :controller => "comments", :action => "increment", :id => comment.id %> 

这就是当方法运行日志数据:

Started GET "/comments/increment/12" for 127.0.0.1 at Wed Aug 24 22:35:39 -0400 2011 
Processing by CommentsController#increment as HTML 
Parameters: {"id"=>"12"} 
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = 12 LIMIT 1 
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1 
AREL (0.2ms) UPDATE "comments" SET "rating" = 9, "updated_at" = '2011-08-25 02:35:39.315081' WHERE "comments"."id" = 12 
Redirected to http://localhost:3000/photos/6 

为什么@ comment.user.rank更新被忽略? 注意:我传入的散列应该没问题;我也尝试传递一个具有相同结果的直接整数。

回答

2

尝试致电@comment.user.save!以及。您正在保存评论,但不是相关的模型。

+0

对!啊...谢谢!我会尽快接受它。 –

+0

没问题!很高兴我能帮上忙! – MrDanA