JaMes
JaMes

Reputation: 607

Update params with passed value

I would like to create a generic method to update certain params of my model.

Example:

@parameter = "name"

User.update(@parameter: "new name here")

So here I would like to update the column "name" with the value "new name here". But this method isn't working. Any Idea of how I should do it?

Upvotes: 1

Views: 742

Answers (1)

spike
spike

Reputation: 10014

You can only use the { x: foo } hash syntax when the keys are literals in the source code (like :x in that example). If you want to use the value of a variable as the key to a hash you have to use the old style hashrocket syntax like this:

  User.update(@parameter => "new name here")

(That's not strictly the only way to do it, see this answer for more: Creating a hash key from a variable in Ruby?)

Upvotes: 1

Related Questions