Hello, I''m looking for a variation on the "save" method, that will only save attributes that do not have errors attached to them. So a model can be updated without being valid overall, and this will still prevent saving invalid data to the database. I''ve been looking through the Rails code for a while now, and I''ve been unable to find anything that might work. Does anyone have an ideas for how I can accomplish this? Thanks for your time, Brady --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hello, I''ve been going through the Rails code today, looking for a method that will allow me to save an invalid record, but only update attributes that do not have errors attached. (i.e. model.errors.on(:attribute) is nil). Any ideas on how to accomplish this would be appreciated. If this isn''t a part of Rails, I think it would be used enough to be a good idea for a patch. Thanks, Brady --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You could solve it by doing something like this? def save_valid_attributes self.valid? self.attributes.each do |k,v| self.update_attribute(k.to_sym, v) unless self.errors.on(k.to_sym) end end Rob Lacey brady8 wrote:> Hello, > > I''m looking for a variation on the "save" method, that will only save > attributes that do not have errors attached to them. > > So a model can be updated without being valid overall, and this will > still prevent saving invalid data to the database. > > I''ve been looking through the Rails code for a while now, and I''ve > been unable to find anything that might work. Does anyone have an > ideas for how I can accomplish this? > > Thanks for your time, > Brady > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
something like def save_valid_attributes valid? update_atrtibutes attributes.inject({}){|k, v, m| m[k] = v unless errors_on(k.to_sym); m} end would save you multiple hits on the db for single attributes. check out Enumerable#inject for more information http://www.ruby-doc.org/core/classes/Enumerable.html#M003160 HTH RSL On Mon, Sep 15, 2008 at 6:29 AM, Rob Lacey <robl-JfOZzmRpUsu/uRbK1X5IkLHvCTsVnGdA@public.gmane.org>wrote:> > You could solve it by doing something like this? > > def save_valid_attributes > self.valid? > self.attributes.each do |k,v| > self.update_attribute(k.to_sym, v) unless self.errors.on(k.to_sym) > end > end > > Rob Lacey > > brady8 wrote: > > Hello, > > > > I''m looking for a variation on the "save" method, that will only save > > attributes that do not have errors attached to them. > > > > So a model can be updated without being valid overall, and this will > > still prevent saving invalid data to the database. > > > > I''ve been looking through the Rails code for a while now, and I''ve > > been unable to find anything that might work. Does anyone have an > > ideas for how I can accomplish this? > > > > Thanks for your time, > > Brady > > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Russell Norris wrote:> def save_valid_attributes > valid? > update_atrtibutes attributes.inject({}){|k, v, m| m[k] = v unless > errors_on(k.to_sym); m} > endAnd note this violates the GUI principle of least surprise. If a customer were changing credit card and billing address at the same time, they don''t need the billing address to stick in the database when the CC bounces. I''m aware the actual problem may be different here... -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
it still violates it in the original code. it just violates it with less db hits. RSL On Mon, Sep 15, 2008 at 8:21 AM, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Russell Norris wrote: > > > def save_valid_attributes > > valid? > > update_atrtibutes attributes.inject({}){|k, v, m| m[k] = v unless > > errors_on(k.to_sym); m} > > end > > And note this violates the GUI principle of least surprise. > > If a customer were changing credit card and billing address at the same > time, > they don''t need the billing address to stick in the database when the CC > bounces. > > I''m aware the actual problem may be different here... > > -- > Phlip > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks, that inject code will work nicely! And yes, I''m a good like interface designer, due to the nature of what I''m working on there is lots of partial updates to data before the model instance is full and valid, and the user is notified that partial updates are saved. Thanks again, I appreciate it. -Brady On Sep 15, 6:34 am, "Russell Norris" <r...-ftMzyaTR+bHNyFkoKTPOtdi2O/JbrIOy@public.gmane.org> wrote:> it still violates it in the original code. it just violates it with less db > hits. > > RSL > > On Mon, Sep 15, 2008 at 8:21 AM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Russell Norris wrote: > > > > def save_valid_attributes > > > valid? > > > update_atrtibutes attributes.inject({}){|k, v, m| m[k] = v unless > > > errors_on(k.to_sym); m} > > > end > > > And note this violates the GUI principle of least surprise. > > > If a customer were changing credit card and billing address at the same > > time, > > they don''t need the billing address to stick in the database when the CC > > bounces. > > > I''m aware the actual problem may be different here... > > > -- > > Phlip--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---