Forgive my extreme noobishness, but why doesn''t the following work? class Wombat < ActiveRecord::Base validates_presence_of :name class String def normalize self.upcase end end def name=(name) write_attribute(:name, name.normalize) end end Of course I''d want to put the embellishment of String in a module, but I''m just trying to get it working. But instead I get:>> w = Wombat.new(:name => "test")NoMethodError: undefined method `normalize'' for "test":String from (irb):25:in `name='' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1672:in `send'' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1672:in `attributes='' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1671:in `each'' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1671:in `attributes='' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1505:in `initialize_without_callbacks'' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/callbacks.rb:225:in `initialize'' from (irb):29:in `new'' from (irb):29 So what''s the right way to do this? I just want to do better than: def name=(name) write_attribute(:name, normalize(name)) end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2007-Sep-01 04:56 UTC
Re: Embellishing String (or whatever) for AR attributes
> class Wombat < ActiveRecord::Base > validates_presence_of :name > > class String > def normalize > self.upcase > end > endMove this String class block outside of the Wombat class block. Eventually move it into say RAILS_ROOT/lib/local/string.rb and then add "require ''local/string''" to your environment.rb or some such. -philip> > def name=(name) > write_attribute(:name, name.normalize) > end > end > > Of course I''d want to put the embellishment of String in a module, but > I''m just trying to get it working. But instead I get: > >>> w = Wombat.new(:name => "test") > NoMethodError: undefined method `normalize'' for "test":String > from (irb):25:in `name='' > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/base.rb:1672:in `send'' > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/base.rb:1672:in `attributes='' > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/base.rb:1671:in `each'' > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/base.rb:1671:in `attributes='' > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/base.rb:1505:in `initialize_without_callbacks'' > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ > active_record/callbacks.rb:225:in `initialize'' > from (irb):29:in `new'' > from (irb):29 > > So what''s the right way to do this? I just want to do better than: > > def name=(name) > write_attribute(:name, normalize(name)) > end > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Sep 1, 12:56 am, Philip Hallstrom <ra...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> Move this String class block outside of the Wombat class block. > > Eventually move it into say RAILS_ROOT/lib/local/string.rb and then add > "require ''local/string''" to your environment.rb or some such. > > -philipThanks. Perfect. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---