Michal Gabrukiewicz
2007-Dec-13 13:18 UTC
automatically strip values of activerecord attributes
i am doing a couple of times the following in my models class User < ActiveRecord::Base def name=(val) write_attribute :name, val.to_s.strip end end quite clear, it strips the name when setting it. now the question: Is it possible to do something like the following .. and if yes how could i achieve it class User < ActiveRecord::Base auto_strip :name, :street end thanks... -- 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 -~----------~----~----~----~------~----~------~--~---
Michael Bleigh
2007-Dec-13 13:42 UTC
Re: automatically strip values of activerecord attributes
This should do the trick: class User < ActiveRecord::Base def self.auto_strip(*args) args.each do |attribute_to_strip| class_eval <<-RUBY def #{attribute_to_strip.to_s}=(value) write_attribute :#{attribute_to_strip.to_s}, value.strip end RUBY end end auto_strip :name, :street end On Dec 13, 2007, at 8:18 AM, Michal Gabrukiewicz wrote:> > i am doing a couple of times the following in my models > > class User < ActiveRecord::Base > def name=(val) > write_attribute :name, val.to_s.strip > end > end > > quite clear, it strips the name when setting it. > now the question: Is it possible to do something like the following .. > and if yes how could i achieve it > > class User < ActiveRecord::Base > auto_strip :name, :street > end > > thanks... > -- > 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 -~----------~----~----~----~------~----~------~--~---
Michal Gabrukiewicz
2007-Dec-13 15:16 UTC
Re: automatically strip values of activerecord attributes
hey that looks good ... this i what i need. thx -- 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 -~----------~----~----~----~------~----~------~--~---