This project is archived and is in readonly mode.
Single row update without a select or knowing id
Reported by Kieran P | January 16th, 2009 @ 01:43 AM | in 2.x
I was looking for a single row update that doesn't use a select. The closest available in Rails in Model.update() but it required an ID, which I don't have. What I do have is the attributes of some of the other fields. I was hoping to use something like this for example:
Model.update({ :email => 'test@test.com' }, { :conditions => { :login => 'someone' })
The resulting SQL would look like this of course:
UPDATE model SET email = 'test@test.com' WHERE login = 'someone';
However, there doesn't seem to be a way to do this in Rails at the moment, and the only way to do it is to run a find_by_login and update_attribute call on the result, but as you know, that makes two queries.
Any ideas when a single update method that doesn't require an id, might be available?
Comments and changes to this ticket
-
Chris Kampmeier March 16th, 2009 @ 01:31 PM
This is already available as
ActiveRecord::Base#update_all
. See the API docs for more info. -
CancelProfileIsBroken May 16th, 2009 @ 05:27 PM
- State changed from new to wontfix
Nothing to do here unless you have a use case that update_all won't handle.
-
Ryan Bigg October 11th, 2010 @ 10:57 AM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>