Constant already initialized


#1

Hi

I’ve used a number of constants within my models to avoid magic
numbers:

class Order < ActiveRecord::Base

#snip

DAYS_ALLOWED_FOR_CHANGE = 2
DAYS_PER_WEEK = 7

#snip

end

However when running tests or starting the mongrel webserver I get
warning messages such as:

…/order.rb:36: warning: already initialized constant
DAYS_ALLOWED_FOR_CHANGE
…/order.rb:37: warning: already initialized constant DAYS_PER_WEEK

How can I track down where this is being reloaded? Should I worry
about it?

Any advice appreciated.

Thanks
Adrian


#2

Constants can’t be reinitialized,
so, Initialize constant while server starts
add this to the end of environment.rb

DAYS_ALLOWED_FOR_CHANGE = 2
DAYS_PER_WEEK = 7

  • Sandip R~

On Fri, Jun 5, 2009 at 3:49 PM, Adrian removed_email_address@domain.invalid
wrote:

DAYS_ALLOWED_FOR_CHANGE = 2
DAYS_ALLOWED_FOR_CHANGE


Ruby on Rails Developer
http://sandip.sosblog.com
http://funonrails.wordpress.com
www.joshsoftware.com


#3

Are you saying that all constants should be defined in the
environment.rb file?


#4

On Jun 5, 11:19 am, Adrian removed_email_address@domain.invalid wrote:

DAYS_PER_WEEK = 7
…/order.rb:37: warning: already initialized constant DAYS_PER_WEEK

How can I track down where this is being reloaded? Should I worry
about it?

I suspect you are using require to manually load order when you don’t
need it.

The easiest thing to do would be to stick

puts caller

at the top of order.rb which will spew the callstack everytime
order.rb is loaded.

Fred


#5

I did as you suggested

puts ‘======>Order’
puts caller

The result was something like this? Any ideas?

redwood:trunk adrian$ rake test:functionals
(in /Users/adrian/Development/Rails/fc/fc2/trunk)
======>Order
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:147:in load_without_new_constant_marking' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ dependencies.rb:147:inload’
/Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:79
/Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:
78:in each' /Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:78 /Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb: 77:ineach’
/Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:77
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in
gem_original_require' /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:inrequire’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:156:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ dependencies.rb:521:innew_constants_in’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:156:in require' /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/tasks/misc.rake:4 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:incall’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:ineach’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in execute' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:ininvoke_with_call_chain’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:607:in
invoke_prerequisites' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:ineach’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in
invoke_prerequisites' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:596:ininvoke_with_call_chain’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:607:in
invoke_prerequisites' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:ineach’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in
invoke_prerequisites' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:596:ininvoke_with_call_chain’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:607:in
invoke_prerequisites' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:ineach’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in
invoke_prerequisites' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:596:ininvoke_with_call_chain’
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/
1.8/monitor.rb:242:in synchronize' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:ininvoke_with_call_chain’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in invoke' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:ininvoke_task’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:ineach’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:instandard_exception_handling’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in top_level' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:inrun’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in
standard_exception_handling' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:inrun’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in load' /usr/bin/rake:19 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby - I"lib:test" "/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/ rake_test_loader.rb" "test/functional/ administration_controller_test.rb" "test/functional/ certifiers_controller_test.rb" "test/functional/ delivery_locations_controller_test.rb" "test/functional/ delivery_runs_controller_test.rb" "test/functional/ farming_methods_controller_test.rb" "test/functional/ home_controller_test.rb" "test/functional/ ingredients_controller_test.rb" "test/functional/ messages_controller_test.rb" "test/functional/ orders_controller_test.rb" "test/functional/ packing_shifts_controller_test.rb" "test/functional/ passwords_controller_test.rb" "test/functional/ payment_types_controller_test.rb" "test/functional/ payments_controller_test.rb" "test/functional/ product_prices_controller_test.rb" "test/functional/ product_types_controller_test.rb" "test/functional/ production_methods_controller_test.rb" "test/functional/ products_controller_test.rb" "test/functional/ products_ingredients_controller_test.rb" "test/functional/ products_production_methods_controller_test.rb" "test/functional/ referral_types_controller_test.rb" "test/functional/ regions_controller_test.rb" "test/functional/ reports_controller_test.rb" "test/functional/roles_controller_test.rb" "test/functional/storage_types_controller_test.rb" "test/functional/ suppliers_controller_test.rb" "test/functional/ suppliers_farming_methods_controller_test.rb" "test/functional/ unit_of_measures_controller_test.rb" "test/functional/ user_actions_controller_test.rb" "test/functional/ user_comments_controller_test.rb" "test/functional/ users_controller_test.rb" "test/functional/ vehicles_controller_test.rb" ======>Order /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:ingem_original_require’
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in require' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ dependencies.rb:158:inrequire’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:265:in require_or_load' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ dependencies.rb:224:independ_on’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:136:in require_dependency' /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:393:inload_application_classes’
/Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:392:in
each' /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:392:inload_application_classes’
/Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:390:in
each' /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:390:inload_application_classes’
/Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:195:in
process' /Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:insend’
/Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in
run' /Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:14 ./test/test_helper.rb:2:inrequire’
./test/test_helper.rb:2
./test/functional/administration_controller_test.rb:1:in require' ./test/functional/administration_controller_test.rb:1 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb: 5:inload’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:
5:in each' /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5 /Users/adrian/Development/Rails/fc/fc2/trunk/app/models/customer.rb:5: warning: already initialized constant LONG_TERM_CUSTOMER_DAYS /Users/adrian/Development/Rails/fc/fc2/trunk/app/models/customer.rb:6: warning: already initialized constant HIGH_VALUE_CUSTOMER_DOLLARS /Users/adrian/Development/Rails/fc/fc2/trunk/app/models/dashboard/ dashboard.rb:35: warning: already initialized constant WEEKS_HISTORY /Users/adrian/Development/Rails/fc/fc2/trunk/app/models/dashboard/ dashboard.rb:36: warning: already initialized constant WEEKS_DISPLAYED /Users/adrian/Development/Rails/fc/fc2/trunk/app/models/dashboard/ dashboard.rb:37: warning: already initialized constant DAYS_PER_WEEK /Users/adrian/Development/Rails/fc/fc2/trunk/app/models/dashboard/ dashboard.rb:38: warning: already initialized constant DAY_NUMBER_ALWAYS_PRESENT_IN_WEEK ======>Order /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/ dependencies.rb:147:inload_without_new_constant_marking’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/
dependencies.rb:147:in load' /Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:79 /Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb: 78:ineach’
/Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:78
/Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:
77:in each' /Users/adrian/Development/Rails/fc/fc2/trunk/config/environment.rb:77 ./test/test_helper.rb:2:inrequire’
./test/test_helper.rb:2
./test/functional/administration_controller_test.rb:1:in require' ./test/functional/administration_controller_test.rb:1 /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb: 5:inload’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:
5:in `each’
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
/Users/adrian/Development/Rails/fc/fc2/trunk/app/models/order.rb:14:
warning: already initialized constant DAYS_ALLOWED_FOR_CHANGE
/Users/adrian/Development/Rails/fc/fc2/trunk/app/models/order.rb:15:
warning: already initialized constant DAYS_PER_WEEK
/Users/adrian/Development/Rails/fc/fc2/trunk/app/models/reports/
report_product_price_list.rb:5: warning: already initialized constant
DESCRIPTION_WORDS_LIMIT
/Users/adrian/Development/Rails/fc/fc2/trunk/app/models/reports/
report_user_dashboard.rb:3: warning: already initialized constant
PRODUCT_COL_WIDTH
/Users/adrian/Development/Rails/fc/fc2/trunk/app/models/reports/
report_user_dashboard.rb:4: warning: already initialized constant
WEEK_COL_WIDTH
/Users/adrian/Development/Rails/fc/fc2/trunk/app/models/reports/
report_user_dashboard.rb:5: warning: already initialized constant
REMAIN_COL_WIDTH
/Users/adrian/Development/Rails/fc/fc2/trunk/app/models/reports/
report_user_dashboard.rb:6: warning: already initialized constant
NUM_WEEKS
Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/
rake_test_loader
Started

On Jun 5, 8:30 pm, Frederick C. removed_email_address@domain.invalid


#6

Yeah this will fix the problem - but I thought that constants should
probably live with their model so that you can use the namespace ie

Order::DAYS_PER_WEEK

Is there a best practice for constants?


#7

If you have bunch of constants then add all contsants in one file say
constants.rb
and put contants.rb file in config/initializers

what you say ??

  • Sandip R~

On Fri, Jun 5, 2009 at 4:00 PM, Frederick C.
<removed_email_address@domain.invalid

wrote:

warning messages such as:

Thanks
Adrian


Ruby on Rails Developer
http://sandip.sosblog.com
http://funonrails.wordpress.com
www.joshsoftware.com


#8

On Jun 5, 11:36 am, Adrian removed_email_address@domain.invalid wrote:

I did as you suggested

puts ‘======>Order’
puts caller

The result was something like this? Any ideas?

Looks like you are requiring order from environment.rb

Fred


#9

I suspect about model wise constant initialization.
what i can suggest is add _constant.rb in config/initializers as
a
good practice.

  • Sandip R~


Ruby on Rails Developer
http://sandip.sosblog.com
http://funonrails.wordpress.com
www.joshsoftware.com


#10

Thanks all

found the problem - doubled up in the environment.rb (as below)

Thanks for the help


#11

You might like to consider using class variables rather than
constants, then they are more obviously associated with their models
and you would not run into the sort of problems you describe.

Colin

2009/6/5 Adrian removed_email_address@domain.invalid:


#12

I have a number of subdirectories to group models together and have
added to following to the environment.rb file

Rails::Initializer.run do |config|

#snip

Include models in sub directories

config.load_paths += Dir["#{RAILS_ROOT}/app/models/*"].find_all { |
f| File.stat(f).directory? }

end

[ “app/models” ].each do |path|
Dir["#{RAILS_ROOT}/#{path}/**/*.rb"].each do |file|
load file
end
end

On Jun 5, 8:52 pm, Frederick C. removed_email_address@domain.invalid