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_cache
in AR association. For example:Rails will do a lot of work without your attention.
For two methods that you mentioned above (
increcement_counter
andincresement
) they are use for different purposes. Theincrecement_counter
is the back magic forcounter_cache
. Theincresement
use for just increase some integer value in the table.