Hi all
Always when I begin to think that I''m getting better and better in
mastering Ruby I stumble upon some strange behavior that I don''t
understand... ;-)
class Country < ActiveRecord::Base
validates_presence_of :abbrevation
validates_presence_of :name
validates_format_of :abbrevation,
:with => /[a-z]{2}/i,
:message => self.error_messages[:not_allowed_chars_in_abbrevation]
def self.error_messages
{ :not_allowed_chars_in_abbrevation => ''Only characters A-Z are
allowed''}
end
end
This gives me the following error:
usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1235:in
`method_missing'': undefined local variable or method
`error_messages''
for Country:Class (NameError)
from
/Users/Josh/Webwork/pgbookings/config/../app/models/country.rb:6
...
And I don''t have a clue what''s wrong here... :-/ Thanks for a
hint. :-)
Josh
--
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
-~----------~----~----~----~------~----~------~--~---
On Mar 22, 8:27 pm, Joshua Muheim <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> class Country < ActiveRecord::Base > validates_format_of :abbrevation, > :with => /[a-z]{2}/i, > :message => self.error_messages[:not_allowed_chars_in_abbrevation] > > def self.error_messages > { :not_allowed_chars_in_abbrevation => ''Only characters A-Z are > allowed''} > end > end > > This gives me the following error: > > usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:12 35:in > `method_missing'': undefined local variable or method `error_messages'' > for Country:Class (NameError) > from > /Users/Josh/Webwork/pgbookings/config/../app/models/country.rb:6Try putting the definition of error_messages above the place where you use it. Chris --~--~---------~--~----~------------~-------~--~----~ 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, this drove me into the right direction.
Anyway, I changed my design a little bit:
class Country < ActiveRecord::Base
validates_presence_of :abbrevation
validates_presence_of :name
def self.error_messages
{ :not_allowed_chars_in_abbrevation => ''Only characters A-Z are
allowed''}
end
def after_validation
@errors.add(:abbrevation,
self.error_messages[:not_allowed_chars_in_abbrevation]) if
!@errors.on(:abbrevation) and @abbrevation !~ /^[a-z]{2}$/i
end
end
Now I''m getting such nasty errors againa! Why? :O
NoMethodError: undefined method `error_messages'' for
#<Country:0x25b3c58>
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in
`method_missing''
/Users/Josh/Webwork/pgbookings/config/../app/models/country.rb:10:in
`after_validation''
...
--
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
-~----------~----~----~----~------~----~------~--~---
Hi -- On 3/22/07, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Thanks, this drove me into the right direction. > > Anyway, I changed my design a little bit: > > class Country < ActiveRecord::Base > validates_presence_of :abbrevation > validates_presence_of :name > > def self.error_messages > { :not_allowed_chars_in_abbrevation => ''Only characters A-Z are > allowed''} > end > > def after_validation > @errors.add(:abbrevation, > self.error_messages[:not_allowed_chars_in_abbrevation]) if > !@errors.on(:abbrevation) and @abbrevation !~ /^[a-z]{2}$/i > end > end > > Now I''m getting such nasty errors againa! Why? :O > > NoMethodError: undefined method `error_messages'' for > #<Country:0x25b3c58> > /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1860:in > `method_missing'' > /Users/Josh/Webwork/pgbookings/config/../app/models/country.rb:10:in > `after_validation''Like most questions about Ruby, it comes down to: 1. an object can have its own methods 2. classes are objects too :-) You''ve defined error_messages as a singleton method for your class (a class method), but then you''re trying to call it on a totally different object, namely an *instance* of your class. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
Thanks a lot for this information. I see, I''m still quite nowhere in really understanding Ruby, but I''m making progress. ;-) Is it possible that in Java on can call class methods (static methods) from the class itself and from an instantiated object? MyClass.staticMethod() => someValue myClassObj.staticMethod() => someValue I''m just wondering... :-) Already some years ago since I used Java. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi -- On 3/22/07, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Thanks a lot for this information. I see, I''m still quite nowhere in > really understanding Ruby, but I''m making progress. ;-) > > Is it possible that in Java on can call class methods (static methods) > from the class itself and from an instantiated object? > > MyClass.staticMethod() => someValue > myClassObj.staticMethod() => someValue > > I''m just wondering... :-) Already some years ago since I used Java.I don''t know (I''m not a Java programmer), but someone probably does :-) In Ruby, you can do: class MyClass def self.some_class_method # ... end def an_instance_method self.class.some_class_method end end Basically, as long as you can get hold of an object that understands what you''re asking it to do (in this case, the object in question is self.class), you can ask it to do it. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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 -~----------~----~----~----~------~----~------~--~---
Yes java and ruby work the same in this manner. You can call static or class level methods from either another static or an instance -Michael http://javathehutt.blogspot.com On Mar 22, 2007, at 3:48 PM, David A. Black wrote:> > Hi -- > > On 3/22/07, Joshua Muheim <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> Thanks a lot for this information. I see, I''m still quite nowhere in >> really understanding Ruby, but I''m making progress. ;-) >> >> Is it possible that in Java on can call class methods (static >> methods) >> from the class itself and from an instantiated object? >> >> MyClass.staticMethod() => someValue >> myClassObj.staticMethod() => someValue >> >> I''m just wondering... :-) Already some years ago since I used Java. > > I don''t know (I''m not a Java programmer), but someone probably does > :-) In Ruby, you can do: > > class MyClass > def self.some_class_method > # ... > end > > def an_instance_method > self.class.some_class_method > end > end > > Basically, as long as you can get hold of an object that understands > what you''re asking it to do (in this case, the object in question is > self.class), you can ask it to do it. > > > David > > -- > Q. What is THE Ruby book for Rails developers? > A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) > (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) > Q. Where can I get Ruby/Rails on-site training, consulting, coaching? > A. Ruby Power and Light, LLC (http://www.rubypal.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 22-mrt-2007, at 21:27, Joshua Muheim wrote:> > Hi all > > Always when I begin to think that I''m getting better and better in > mastering Ruby I stumble upon some strange behavior that I don''t > understand... ;-) > > class Country < ActiveRecord::Base > validates_presence_of :abbrevation > validates_presence_of :name > validates_format_of :abbrevation, > :with => /[a-z]{2}/i, > :message => self.error_messages[:not_allowed_chars_in_abbrevation] > > def self.error_messages > { :not_allowed_chars_in_abbrevation => ''Only characters A-Z are > allowed''} > end > endNot too sure about the R(oR) error, but why don''t you use :message => "Only etc.." ? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Well I wouldn''t say that they work exactly the same... You
can''t call a
class method on an instance directly (foo.some_class_method), but you
can call it on the class of the instance (foo.class.some_class_method).
In java, you can call the static method on the instance but it''s a
compiler warning ("Static method should be accessed in a static
way")...
you''re supposed to call static methods on the class itself.
b
Michael Kovacs wrote:> Yes java and ruby work the same in this manner. You can call static
> or class level methods from either another static or an instance
>
> -Michael
> http://javathehutt.blogspot.com
>
> On Mar 22, 2007, at 3:48 PM, David A. Black wrote:
>
>> Hi --
>>
>> On 3/22/07, Joshua Muheim
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
>>> Thanks a lot for this information. I see, I''m still quite
nowhere in
>>> really understanding Ruby, but I''m making progress. ;-)
>>>
>>> Is it possible that in Java on can call class methods (static
>>> methods)
>>> from the class itself and from an instantiated object?
>>>
>>> MyClass.staticMethod() => someValue
>>> myClassObj.staticMethod() => someValue
>>>
>>> I''m just wondering... :-) Already some years ago since I
used Java.
>> I don''t know (I''m not a Java programmer), but someone
probably does
>> :-) In Ruby, you can do:
>>
>> class MyClass
>> def self.some_class_method
>> # ...
>> end
>>
>> def an_instance_method
>> self.class.some_class_method
>> end
>> end
>>
>> Basically, as long as you can get hold of an object that understands
>> what you''re asking it to do (in this case, the object in
question is
>> self.class), you can ask it to do it.
>>
>>
>> David
>>
>> --
>> Q. What is THE Ruby book for Rails developers?
>> A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
>> (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
>> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
>> A. Ruby Power and Light, LLC (http://www.rubypal.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
-~----------~----~----~----~------~----~------~--~---
Bart Zonneveld wrote:> On 22-mrt-2007, at 21:27, Joshua Muheim wrote: > >> validates_format_of :abbrevation, >> :with => /[a-z]{2}/i, >> :message => self.error_messages[:not_allowed_chars_in_abbrevation] >> >> def self.error_messages >> { :not_allowed_chars_in_abbrevation => ''Only characters A-Z are >> allowed''} >> end >> end > > Not too sure about the R(oR) error, but why don''t you use :message => > "Only etc.." ?Because I''m using Unit tests with something like that: assert_equal(@country.errors.on(:abbrevation), Country.error_messages[:not_allowed_chars_in_abbrevation]) That way I can e.g. i18n the project without having hard programmed unit tests. -- 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 -~----------~----~----~----~------~----~------~--~---