Forum: Ruby on Rails Constant already initialized

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8bc5d7d843efe863b249b0a593803e70?d=identicon&s=25 Adrian (Guest)
on 2009-06-05 12:20
(Received via mailing list)
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
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-05 12:26
(Received via mailing list)
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 <AdrianLloydSmith@gmail.com>
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
8bc5d7d843efe863b249b0a593803e70?d=identicon&s=25 Adrian (Guest)
on 2009-06-05 12:28
(Received via mailing list)
Are you saying that all constants should be defined in the
environment.rb file?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-06-05 12:31
(Received via mailing list)
On Jun 5, 11:19 am, Adrian <AdrianLloydSm...@gmail.com> 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
8bc5d7d843efe863b249b0a593803e70?d=identicon&s=25 Adrian (Guest)
on 2009-06-05 12:37
(Received via mailing list)
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
.................................

On Jun 5, 8:30 pm, Frederick Cheung <frederick.che...@gmail.com>
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-05 12:38
(Received via mailing list)
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 Cheung
<frederick.cheung@gmail.com
> wrote:

> >
> > warning messages such as:
>
> >
> > Thanks
> > Adrian
> >
>


--
Ruby on Rails Developer
http://sandip.sosblog.com
http://funonrails.wordpress.com
www.joshsoftware.com
8bc5d7d843efe863b249b0a593803e70?d=identicon&s=25 Adrian (Guest)
on 2009-06-05 12:42
(Received via mailing list)
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?
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-05 12:51
(Received via mailing list)
I suspect about model wise constant initialization.
what i can suggest is add <MODEL>_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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-06-05 12:53
(Received via mailing list)
On Jun 5, 11:36 am, Adrian <AdrianLloydSm...@gmail.com> 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
8bc5d7d843efe863b249b0a593803e70?d=identicon&s=25 Adrian (Guest)
on 2009-06-05 12:56
(Received via mailing list)
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 Cheung <frederick.che...@gmail.com>
8bc5d7d843efe863b249b0a593803e70?d=identicon&s=25 Adrian (Guest)
on 2009-06-05 13:13
(Received via mailing list)
Thanks all

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

Thanks for the help
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-06-05 22:55
(Received via mailing list)
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 <AdrianLloydSmith@gmail.com>:
This topic is locked and can not be replied to.