Adrian1
(Adrian)
June 5, 2009, 10:19am
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
Sandip
(Sandip)
June 5, 2009, 10:25am
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
Adrian1
(Adrian)
June 5, 2009, 10:27am
3
Are you saying that all constants should be defined in the
environment.rb file?
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?
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
Adrian1
(Adrian)
June 5, 2009, 10:36am
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:in `load'
/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:in `each'
/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:in `require'
/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:in `new_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:in `call'
/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:in `each'
/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:in
`invoke_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:in
`invoke_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:in `each'
/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:in
`invoke_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:in
`invoke_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:in `each'
/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:in
`invoke_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:in
`invoke_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:in `each'
/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:in
`invoke_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:in
`invoke_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:in
`invoke_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:in `each'
/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:in
`standard_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:in `run'
/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:in `run'
/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:in
`gem_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:in `require'
/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:in `depend_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:in
`load_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:in
`load_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:in
`load_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:in
`send'
/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:in `require'
./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:in `load'
/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:in `load_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:in `each'
/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:in `require'
./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:in `load'
/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
.................................
Sandip
(Sandip)
June 5, 2009, 10:37am
6
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 ??
Adrian1
(Adrian)
June 5, 2009, 10:42am
7
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?
Sandip
(Sandip)
June 5, 2009, 10:49am
8
I suspect about model wise constant initialization.
what i can suggest is add _constant.rb in config/initializers as a good practice.
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
Adrian1
(Adrian)
June 5, 2009, 10:55am
10
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
Adrian1
(Adrian)
June 5, 2009, 11:13am
11
Thanks all
found the problem - doubled up in the environment.rb (as below)
Thanks for the help
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