def update_attribute_if_changed(name, value)
self[name] != value ? update_attribute(name, value) : true
end
end
This doesn’t address my original problem though, right? I mean in the
end you still call update_attribute which generates the full update
SQL, not just for one field. Or am I missing something here…
This doesn’t address my original problem though, right? I mean in the
end you still call update_attribute which generates the full update
SQL, not just for one field. Or am I missing something here…
Yes, as I wrote, unless you do custom SQL, Rails always saves entire
records.
The code I gave was just a way to cut out unnecessary saves in certain
circumstances.
I’m not sure whether entire records are saved to simplify the Rails
code,
or because of some tricky issues that would arise if attributes could
be updated individually. At the least you’d probably have to add a
dirty
bit to each record, or to each attribute in each record.
–
We develop, watch us RoR, in numbers too big to ignore.
def update_attribute(name, value)
send(name.to_s + '=', value)
save
end
def save
raise ActiveRecord::ReadOnlyRecord if readonly?
create_or_update
end
The call to ‘create_or_update’ will trigger either object#create or
object#update, each of which contains the SQL code that persists the
attributes in the object. You could probably override
update_attribute, and have it call a new method
‘create_or_update_lone_attribute’ instead of ‘save’. Doesnt look like
that would break anything.
On the other hand, why? Unless you have some huge binary columns, the
db will update n columns in a record just about as quick as a single
column.
cheers
Gerret
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.