I'm trying to put into practise using skinny controllers and fat models, however in order to do this I find myself needing to pass additional variables into the .create method.
So for example where I had:
@app = App.new(app_params)
def app_params
params.require(:app).permit( :name, :description)
end
Now I want to also pass along say, a session held user_id, and a reference to a permission group. So hopefully something like the following pseudo code:
@app = App.create(app_params, session[:user_id], app_permission_id)
The user_id and app_permission_id would not be persisted but would be available I'd hope somewhere like in the after_create callback within the model, so I can then do some further work, for example creating a log entry of the creation, associated to the calling user.
I'm not so keen on just adding them in the form as hidden fields (to hide implementation from users). I think there is a possible approach using .merge? if that's the way, the I guess I pull them out using a virtual attribute.
Any advise on the best/cleanest approach would be much appreciated. I think I'd prefer to override the .create method somehow (but only in terms of the cleanest implementation from the controller side).
If you are app_params should always contain user_id and app_permission_id
you should do it this way
def app_params
params.require(:app).permit( :name, :description).merge(user_id: session[:user_id], app_permission_id: app_permission_id)
end
Else if it should not contain this all the time
you should do it this way
app_params.merge(user_id: session[:user_id], app_permission_id: app_permission_id) #as mentioned by Vapire in his answer
Yes, this should be possible with merge
def create
@app = App.new(app_params.merge(user_id: session[:user_id], app_permission_id: app_permission_id))
end
Of course you'd need virtual attributes user_id
and app_permission_id
in your model.
Also see this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With