Reputation: 5882
So I understand that you can update some models using Model.update(ids, values)
where ids
represent the ID and values
represent the actual changes. I do also understand that you can pass in a hash, such as {1 => {column_name: "new value"}, 2 => {column_name: "new value"}}
which works just fine.
However, what if, instead of IDs, I wanted to use another column such as uuid
? Can I use the .update
method in such a way to where it would do something like Model.update(uuid: hash.keys, hash.values)
?
It doesn't appear that I can do this with this method, but is there another way that I can do this so that I don't have to iterate through every single key and value in my long array (which contains thousands of keys and values)
This is what happens when I try to implement what I would like to do:
[2] pry(#<MyWorker>)> test = {"b7d720f984abeda37836d07b2147560ce06fb4d7e30fe8d59c1fe610cb440bbf" => {:protocol => "udp", :port => 5}}
=> {"b7d720f984abeda37836d07b2147560ce06fb4d7e30fe8d59c1fe610cb440bbf"=>{:protocol=>"udp", :port=>5}}
[3] pry(#<MyWorker>)> Port.update(uuid: test.keys, test.values)
SyntaxError: unexpected ')', expecting =>
...e(uuid: test.keys, test.values)
... ^
[3] pry(#<MyWorker>)>
Upvotes: 1
Views: 34
Reputation: 3036
As Sebastian Palma said, you can do this using a where
clause before the update
action like so:
Port.where(uuid: test.keys).update(test.values)
Upvotes: 1