There are two methods to increment an attribute in Rails:
Instance-level: http://apidock.com/rails/ActiveRecord/Base/increment!
Class-level: http://apidock.com/rails/ActiveRecord/Base/increment_counter/class
I want to update a counter attribute on my Post model that saves the number of comments for a post.
Is any of the two better for my use case?
I will use it with a PostgreSQL database.
For your purpose, I think that you should use an attribute like
:counter_cachein AR association. For example:Rails will do a lot of work without your attention.
For two methods that you mentioned above (
increcement_counterandincresement) they are use for different purposes. Theincrecement_counteris the back magic forcounter_cache. Theincresementuse for just increase some integer value in the table.