I’m a newbie to Rails, and I was thinking in a good solution to set protected attributes in my models before saving them, but I can’t find out a proper way of doing it.
Lets say I have Users, who belong to Companies, and these companies can have many Clients. Before saving a new Client, I want to set its company_id, getting it from the current_user’s company_id that is stored in the session (I’m using Devise).
I was thinking about using a model callback, as before_create or whatever, but I then I realized that my Rspec model tests may fail, because there will be no current_user while running them…
Is there a best practice of setting this parameters that have to be set automatically?
Thanks in advance!
Simply set it in the controller.
If you set it in the model callbacks, there are some disadvantages.