I sometimes need to update attributes in mass after initialization without having to save the record (what #update_attributes does). This doesn''t seem to be possible in any nice way right now. For example, @obj.attributes.merge!(:field => ''new text'') doesn''t stick. I''ve been experimenting with this method and it seems to do the trick: module ActiveRecord class Base def merge_attributes!(attributes = {}) attributes.each_pair do |att, value| self.send("#{att}=", value) if self.respond_to?("#{att}=") end self.attributes end end end Anyone else find that useful? Or harmful? If it makes sense to others ill patch it up. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Hey, isn''t that the same as doing: model.attributes = {:subset => :of, :all => :attributes} Regards, Trevor On 20-Sep-07, at 5:27 PM, Joe Noon wrote:> > I sometimes need to update attributes in mass after initialization > without having to save the record (what #update_attributes does). > This doesn''t seem to be possible in any nice way right now. For > example, @obj.attributes.merge!(:field => ''new text'') doesn''t stick. > > I''ve been experimenting with this method and it seems to do the trick: > > module ActiveRecord > class Base > def merge_attributes!(attributes = {}) > attributes.each_pair do |att, value| > self.send("#{att}=", value) if self.respond_to?("#{att}=") > end > self.attributes > end > end > end > > Anyone else find that useful? Or harmful? If it makes sense to others > ill patch it up. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
> isn''t that the same as doing: > > model.attributes = {:subset => :of, :all => :attributes}Apparently so, thanks for point it out. I was mistakenly treating attributes as if it was just a hash, not a method. Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---