I am building a simple ranking system. When a comment is voted up, the user’s rank gets a boost. The @comment.rating gets updated and saved as it should. However, the @comment.user.rank does not. It does not report any errors, it just never attempts to update or save it.
This is the method in my Comments controller:
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
This is called with this link_to:
<%= link_to "+", :controller => "comments", :action => "increment", :id => comment.id %>
And this is the log data when the method is run:
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
Any thoughts as to why the @comment.user.rank update is getting ignored?
NOTE: The hash I’m passing in should be fine; I’ve also tried passing in a straight integer with the same result.
Try calling
@comment.user.save!as well. You are saving the comment, but not the associated model.