Imagine we are building a quiz app and we'd like all users to start out with 0 points. We can achieve it with a number of ways, one of which would be setting a default value to the user instance.
This method will set the default value only if it's
def init self.points = 0 if self.points.nil? end
after_initialization method at the User class. Now your code should look as follows:
class User < ApplicationRecord after_initialize :init def init self.points = 0 if self.points.nil? end end