1

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?

Richard-Degenne
  • 2,892
  • 2
  • 26
  • 43
jacques Mesnet
  • 607
  • 3
  • 10

1 Answers1

1

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

spike
  • 9,794
  • 9
  • 54
  • 85