Hi all, I''m trying to override some method of a class. I''ve created an acts_as_money method. The first parameter is the method name that should be overridden. For example: I have a model with an attribute "amount". I want to be able to do this: acts_as_money :amount Then the amount method should be aliased to an already defined method in the plugin. I''ve got this code now: http://pastie.caboo.se/63676 But then I get this error: NameError: undefined method `amount'' for class `Declaration'' And the Declaration model HAS a method amount. Does anyone now how I can solve this problem? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
double check the error and make sure its talking about an instance method and not a Class method which should not exist -- 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 -~----------~----~----~----~------~----~------~--~---
maybe this would be simpler def acts_as_money(name, options = {}) define_method(:"#{name}", "def amount @amount end") include LeonB::Acts::Money::InstanceMethods extend LeonB::Acts::Money::SingletonMethods 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 -~----------~----~----~----~------~----~------~--~---
This is the precise error:>> dec = Declaration.newNameError: undefined method `amount'' for class `Declaration'' from ./script/../config/../config/../vendor/plugins/ acts_as_money/lib/acts_as_money.rb:12:in `instance_method'' from ./script/../config/../config/../vendor/plugins/ acts_as_money/lib/acts_as_money.rb:12:in `acts_as_money'' from ./script/../config/../config/../app/models/declaration.rb: 19 from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:203:in `load_without_new_constant_marking'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:203:in `load_file'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:342:in `new_constants_in'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:202:in `load_file'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:94:in `require_or_load'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:248:in `load_missing_constant'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:452:in `const_missing'' from /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/ active_support/dependencies.rb:464:in `const_missing'' from (irb):1>>On May 23, 2:49 am, Keynan Pratt <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> double check the error and make sure its talking about an instance > method and not a Class method which should not exist > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
On 5/22/07, LeonB <leon-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote:> > Hi all, > > I''m trying to override some method of a class. I''ve created an > acts_as_money method. The first parameter is the method name that > should be overridden. For example: > > I have a model with an attribute "amount". I want to be able to do > this: > acts_as_money :amount > > Then the amount method should be aliased to an already defined method > in the plugin. > I''ve got this code now: > http://pastie.caboo.se/63676 > > But then I get this error: NameError: undefined method `amount'' for > class `Declaration'' > > And the Declaration model HAS a method amount. Does anyone now how I > can solve this problem? > > > > >Actually, there is no amount method in the Declaration class. Rails uses method_missing to read the attributes. If you want to do this, just define the method. In order to read the amount column''s value, use read_attribute. define_method(:amount) { "$#{read_attribute(:amount)}" } Let''s say you need to provide the raw column value for some reason. read_attribute is private so you can''t use it, plus it breaks encapsulation, plus it''s just ugly :) (d.send :read_attribute, :amount. bleh) Go ahead and define an accessor method that does read_attribute for you: def acts_as_money(name, options = {}) define_method(:old_amount) { read_attribute :amount } define_method(:amount) { "$#{old_amount}" } end hth Pat --~--~---------~--~----~------------~-------~--~----~ 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 Pat! I never guessed that of method_missing. Makes sense now. On May 23, 9:46 pm, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/22/07,LeonB<l...-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote: > > > > > > > Hi all, > > > I''m trying to override some method of a class. I''ve created an > > acts_as_money method. The first parameter is the method name that > > should be overridden. For example: > > > I have a model with an attribute "amount". I want to be able to do > > this: > > acts_as_money :amount > > > Then the amount method should be aliased to an already defined method > > in the plugin. > > I''ve got this code now: > >http://pastie.caboo.se/63676 > > > But then I get this error: NameError: undefined method `amount'' for > > class `Declaration'' > > > And the Declaration model HAS a method amount. Does anyone now how I > > can solve this problem? > > Actually, there is no amount method in the Declaration class. Rails > uses method_missing to read the attributes. > > If you want to do this, just define the method. In order to read the > amount column''s value, use read_attribute. > > define_method(:amount) { "$#{read_attribute(:amount)}" } > > Let''s say you need to provide the raw column value for some reason. > read_attribute is private so you can''t use it, plus it breaks > encapsulation, plus it''s just ugly :) (d.send :read_attribute, > :amount. bleh) > > Go ahead and define an accessor method that does read_attribute for you: > > def acts_as_money(name, options = {}) > define_method(:old_amount) { read_attribute :amount } > define_method(:amount) { "$#{old_amount}" } > end > > hth > > Pat--~--~---------~--~----~------------~-------~--~----~ 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 Pat! I never guessed that of method_missing. Makes sense now. On May 23, 9:46 pm, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/22/07,LeonB<l...-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote: > > > > > > > Hi all, > > > I''m trying to override some method of a class. I''ve created an > > acts_as_money method. The first parameter is the method name that > > should be overridden. For example: > > > I have a model with an attribute "amount". I want to be able to do > > this: > > acts_as_money :amount > > > Then the amount method should be aliased to an already defined method > > in the plugin. > > I''ve got this code now: > >http://pastie.caboo.se/63676 > > > But then I get this error: NameError: undefined method `amount'' for > > class `Declaration'' > > > And the Declaration model HAS a method amount. Does anyone now how I > > can solve this problem? > > Actually, there is no amount method in the Declaration class. Rails > uses method_missing to read the attributes. > > If you want to do this, just define the method. In order to read the > amount column''s value, use read_attribute. > > define_method(:amount) { "$#{read_attribute(:amount)}" } > > Let''s say you need to provide the raw column value for some reason. > read_attribute is private so you can''t use it, plus it breaks > encapsulation, plus it''s just ugly :) (d.send :read_attribute, > :amount. bleh) > > Go ahead and define an accessor method that does read_attribute for you: > > def acts_as_money(name, options = {}) > define_method(:old_amount) { read_attribute :amount } > define_method(:amount) { "$#{old_amount}" } > end > > hth > > Pat--~--~---------~--~----~------------~-------~--~----~ 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 Pat! I never guessed that of method_missing. Makes sense now. On May 23, 9:46 pm, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/22/07,LeonB<l...-uqieUXqmKe0iy20DiJE0iw@public.gmane.org> wrote: > > > > > > > Hi all, > > > I''m trying to override some method of a class. I''ve created an > > acts_as_money method. The first parameter is the method name that > > should be overridden. For example: > > > I have a model with an attribute "amount". I want to be able to do > > this: > > acts_as_money :amount > > > Then the amount method should be aliased to an already defined method > > in the plugin. > > I''ve got this code now: > >http://pastie.caboo.se/63676 > > > But then I get this error: NameError: undefined method `amount'' for > > class `Declaration'' > > > And the Declaration model HAS a method amount. Does anyone now how I > > can solve this problem? > > Actually, there is no amount method in the Declaration class. Rails > uses method_missing to read the attributes. > > If you want to do this, just define the method. In order to read the > amount column''s value, use read_attribute. > > define_method(:amount) { "$#{read_attribute(:amount)}" } > > Let''s say you need to provide the raw column value for some reason. > read_attribute is private so you can''t use it, plus it breaks > encapsulation, plus it''s just ugly :) (d.send :read_attribute, > :amount. bleh) > > Go ahead and define an accessor method that does read_attribute for you: > > def acts_as_money(name, options = {}) > define_method(:old_amount) { read_attribute :amount } > define_method(:amount) { "$#{old_amount}" } > end > > hth > > Pat--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---