Kumar Nikhil
Kumar Nikhil

Reputation: 125

Rails Params Merge in create action

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.

Upvotes: 0

Views: 531

Answers (3)

LihnNguyen
LihnNguyen

Reputation: 865

You use merge for user_params.

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

Upvotes: 0

Divyarajsinh
Divyarajsinh

Reputation: 44

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

Upvotes: 1

Mehmet Adil İstikbal
Mehmet Adil İstikbal

Reputation: 600

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

Upvotes: 1

Related Questions