CONST initialization...

J

Jean Nibee

How come this works when I access Orders::pAYMENT_TYPES
class Order < ActiveRecord::Base
PAYMENT_TYPES = [
[ "Check", "check" ],
[ "Credit card", "cc" ],
[ "Purchase order", "po" ]
]

has_many :line_items

validates_presence_of :name, :address, :email, :pay_type
validates_inclusion_of :pay_type, :in => PAYMENT_TYPES.map {|disp,
value| value}

end
<<<<<<<<<< CODE

But this doesn't:
class Order < ActiveRecord::Base
has_many :line_items

validates_presence_of :name, :address, :email, :pay_type
validates_inclusion_of :pay_type, :in => PAYMENT_TYPES.map {|disp,
value| value}

PAYMENT_TYPES = [
[ "Check", "check" ],
[ "Credit card", "cc" ],
[ "Purchase order", "po" ]
]

end
<<<<<<<<<< CODE

Error thrown:
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:477:in
`const_missing'

I have moved the PAYMENT_TYPES map around and it even is okay if it's
after the has_many :item_lines accessor, but not if it's after the
'validation' accessors.

Thanks.
 
R

Rob Biedenharn

How come this works when I access Orders::pAYMENT_TYPES

Error thrown:
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:477:in
`const_missing'

I have moved the PAYMENT_TYPES map around and it even is okay if it's
after the has_many :item_lines accessor, but not if it's after the
'validation' accessors.

Thanks.

I think you've answered your own question, but you might not
understand why. The ruby interpreter is "executing" each line within
the class definition as it is encountered. If there's no
PAYMENT_TYPES when the validates_inclusion_of line is reached, it
really doesn't matter that it *does* appear later in the class
definition.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jean Nibee

Rob said:
I think you've answered your own question, but you might not
understand why. The ruby interpreter is "executing" each line within
the class definition as it is encountered. If there's no
PAYMENT_TYPES when the validates_inclusion_of line is reached, it
really doesn't matter that it *does* appear later in the class
definition.

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)

Doh..

Sorry I"m still in 'vm' world w/ my Java code thinking that a Ruby class
is ONLY a definition, I forget to realize it's being interpreted as it's
being read.

Thanks for the wake up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top