Emmanuel Orozco
Emmanuel Orozco

Reputation: 389

Easy way to save items with params in rails

I have my params array like this:

params[:nameCool] = "text1"
params[:nameNotCool] = "text2"

I save my model like

o = Option.new
o.nameCool = params[:nameCool]
o.nameNotCool = params[nameNotCool]

As you can see the params key has the same name as the new Option method.

Is there any way to do this faster (mapping the attribute with the params key?)

Upvotes: 0

Views: 1071

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118299

If your params Hash is too long, and you want to collect only few of them, you can use Hash#slice method.

o = Option.new(params.slice(:name_cool, :name_not_cool))
o.save

Upvotes: 0

bhanu
bhanu

Reputation: 2460

I think this is what you are looking for

o = Option.new(nameCool: params[:nameCool], nameNotCool: params[:nameNotCool])

Upvotes: 1

Related Questions