I have a User model and I have a Company model. A Company has many Users. A User has one Company. A User must always have an associated Company.
There are two ways a User can be created:
- Directly via the ‘sign up’ link on the website
- Via an invitation sent from another user.
If the user signs up directly from the website then they are deemed to be the first user for a new Company.
At the moment I’m taking in the name of the Company as part of the Sign Up form and I’m using the before_create callback in the User model to do something like:
before_create :create_company def create_company unless self.company self.company = Company.create!(name: company_name) end end
When a user is invited then devise_invitable creates a new User model and at that point I can set the company attribute of the new invitee to be the same of the inviter…
Finally, the question, is the before_create callback a good place for something like creating a required association. Is there some better way of doing it? For some reason I don’t like callbacks, I especially dont’ like the idea of a callback that creates a new model, but I’m unsure of a better way.
tl;dr Is it good practise to create a new model in the before_create callback of a different model?
Thanks for reading, and for any advice.