Rails 3 - modifying an attribute of other model after model.save -
unfortunately i'm still rails beginner, so, though thought , tried different approaches, didn't work want , have ask again.
i have rest comment vote mechanism thumbs , down each comment. works fine, each handled counter_cache count. now, based on these thumbs , down votes, want calculate plusminus value each comment, thumbs_up-votes - thumbs_down-votes. although i'm not sure if it's efficient way deal that, planning have plus-minus value integer attribute of comment model (whereas thumbs , down own models). so, want is, when thumbs_up saved, comment's plusminus attr automatically should += 1, , respectively thumbs_down.save a -= 1.
how can issue such action within thumbs_up controller? need modify form_for or approach wrong?
is there after_save callback deal attribute of different model?
from you've given, it's hard tell. i'd if need show comment's "thumbs up" , "thumbs down" independently, store them fields comment model. then, making helper method in comment model comment's rating:
def rating thumbs_up - thumbs_down end edit: new comment, i'd still make helper method rather field.
#models/comment.rb def rating thumbs_up.all.length - thumbs_down.all.length #or whatever way want end
Comments
Post a Comment