I have an id of a record that may or may not exist in the database.
What i want to do is update an attribute of the record in the database only if it exists, otherwise ignore it.
Now i know i can do something like:
model = Model.find_by_id(id)
if model
model.update_attribute(something: something)
end
But i was wondering if there's something nicer that doesn't involve 2 queries?
Model.update_all({:something => 'something'}, {:id => id})
For eg:
User.update_all({:first_name => 's'}, {:id => 1000})
will produce the query
UPDATE "users" SET "first_name" = 's' WHERE "users"."id" = 1000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With