Topic: Update A Single Column

Is there a way to update a single column without reading the entire row from the database and then saving everything back? I know MySQL can do this by simply calling "UPDATE table SET column_name = value". How do I do this in Rails?

Re: Update A Single Column

I'm not sure but you could always just use an sql statment in rails

[code=]find_by_sql [

--

Re: Update A Single Column

Are the updates occuring on any sort of data that could be updated by more than one user at once? What you generally should be doing when you UPDATE is read the current data, check that the current data matches what the user sees as 'old' data, then run the update. It is slower, but it is one of the easiest ways to support concurrenct.

Re: Update A Single Column

SupaPuerco wrote:

Is there a way to update a single column without reading the entire row from the database and then saving everything back? I know MySQL can do this by simply calling "UPDATE table SET column_name = value". How do I do this in Rails?

I think what you want is update_all:

MyModel.update_all("foo='bar'", "id=#{some_id}")

I don't recommend doing this unless you need the performance gain as it bypasses validation and callbacks.

Railscasts - Free Ruby on Rails Screencasts