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 extended ActiveRecord with this:
def update_attributes_with_sql(attrs) # allows you to update
individual columns without saving the whole record (also much faster)
self.class.update_with_sql(id, attrs)
self.attributes = attrs
end
def self.update_with_sql(id, attrs)
conditions = id.is_a?(Array) ? "id IN (#{id.join('',
'')})" :
"id=#{id}"
update_all sanitize_sql_hash_for_set(attrs), conditions
end
def self.sanitize_sql_hash_for_set(attrs)
# just need to replace the join with '', ''instead of
AND
conditions = attrs.map do |attr, value|
"#{table_name}.#{connection.quote_column_name(attr)}
#{attribute_condition(value)}"
end.join('', '')
replace_bind_variables(conditions, attrs.values)
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Lionel,
That''s a good point you make. It''s true, that if you are
saving form
data, validations are super important.
Im not saying to replace update_attributes, but sometimes I just want a
lean straight-to-the-point insert into the database.
I guess you can never be completely sure about needing validation for
the attributes you are updating, but if you are pretty certain about
what you are inserting, it can save a lot of overhead. For example, if I
want to update {:updated_at=>Time.now}, you don''t need to resave all
the
objects.
Lionel Bouton wrote:> Aryk Grosz wrote the following on 24.08.2007 19:36 :
>> 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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---