I’d like to generate unique, anonymous usernames when creating an instance of my User model. Ideally, it would be set by default if I don’t specify any username.
So for example when I do User.create!() I’d like to have the username column set to ‘anonymous123’, where 123 is a unique number taken from a sequence (the value of the id column for that instance would be ok).
Combining the examples from a couple of the answers would do it
I have used before_create here because I would expect you only to have to set an anonymous username before you create the User for the first time. The before_save will be called every time the model is saved and could be unnecessary overhead, but either will work