update_attributes...more efficient replacement

Aryk Grosz wrote the following on 24.08.2007 19:36 :

I was browsing through the Rails Docs, looking for a better way to do updates.

When you use update_attributes, it saves all the attributes from the object. I have some objects with very large text fields which means a lot data going forth and back.

I was looking for a cleaner way. I created a wrapper for the update_all method, which uses straight sql to do updates. So now, I can do user.update_attributes_with_sql(:some_field=>some_value), like I would with update_attributes, only it updates only the fields that I want.

Am I reinventing the wheel here? I couldn't find any function to this in AR.    I toyed with this idea too but didn't follow through because of validation.

Without proper locking you should know that you break one important assumption AR does when validating: the in-memory object validated is actually what will be stored in DB. Your current code doesn't support validation and probably won't properly unless you add at least optimistic locking support.

Lionel.