Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update query with where clause

very simple question - I want to update one record only that matches 2 parameters.

Should I be doing it like this:

Model.where(:email =>"[email protected]",:code => "chejd").update(:password => "password").first

I have found examples using update_all but I only want to update a maximum of 1 record.

like image 684
tommyd456 Avatar asked Sep 25 '13 17:09

tommyd456


1 Answers

Check if this works with your DBMS and DB adapter:

Model.where(conditions).limit(1).update_all(changes) # => 1

Update:

The limit() and update_all() combo is an example in the docs, so it's probably supported by most DB adapters.

like image 67
kristinalim Avatar answered Sep 23 '22 17:09

kristinalim