Home > Enterprise >  Rails Params Merge in create action
Rails Params Merge in create action

Time:07-15

I have a controller, I need to merge one column that is not there in database. Actually that column name I should not add in db and I should not add migration for this.

This is my create action:

   User.new(user_params).merge(first_name: "xyz")

This is my user_params method:

   def user_params
      params.permit(:id, :name :email_id)
   end

but merge is not working. Could any one help me in this. Thanks in advance.

CodePudding user response:

You can merge first_name to the params or set it after initiated the User

def user_params
  params.permit(:id, :name, :email_id).merge(first_name: 'xyz')
end

or:

user = User.new(user_params)
user.first_name = 'xyz'

Edit: If User table does not have the first_name column you can't do what you want

CodePudding user response:

You can use active storage and give relation like user has_one:first_name and you can add first_name without adding it in your user model/table

 def user_params
      params.permit(:id, :name :email_id)
   end

CodePudding user response:

You use merge for user_params.

 User.new(user_params.merge(first_name: "xyz"))
  • Related