Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Single row update without a select or knowing id

#1769

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?

Reported by Kieran P · January 16th, 2009 @ 01:43 AM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: Low

Activity

  1. Chris Kampmeier
    Chris Kampmeier

    This is already available as ActiveRecord::Base#update_all. See the API docs for more info.

    March 16th, 2009 @ 01:31 PM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to wontfix

    Nothing to do here unless you have a use case that update_all won't handle.

    May 16th, 2009 @ 05:27 PM

  3. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:57 AM

  4. Jeff Kreeftmeijer