Rails - passing additional variables on .create

ruby-on-rails, ruby-on-rails-4

Solution

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

Problem

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).

Original source

Related problems