开发者

Ruby on Rails 3: increment value using sql

开发者 https://www.devze.com 2023-04-06 13:29 出处:网络
I\'m doign a voti开发者_运维技巧ng application and I need to update votes count for this I want to use update SQL, because in other cases voices may be lost (AR generates update statement with value,

I'm doign a voti开发者_运维技巧ng application and I need to update votes count for this I want to use update SQL, because in other cases voices may be lost (AR generates update statement with value, but what if previous request already incremented column value?).

Ho do I do that?


You can use increment_counter which does a direct update in the database without caring about the actual value of your Active Record contents:

Vote.increment_counter :vote_value, 5


Try something like:

Movie.update_counters id, :like_count => 1
0

精彩评论

暂无评论...
验证码 换一张
取 消