# Inside or outside of Rails::Initializer block

**URL:** <https://www.ruby-forum.com/t/inside-or-outside-of-rails-initializer-block/170986>\
**Category:** Rails\
**Created:** [June 24, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/inside-or-outside-of-rails-initializer-block/170986 "2009-06-24T22:15:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [June 24, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/inside-or-outside-of-rails-initializer-block/170986/1 "2009-06-24T22:15:26Z")

</div>

Please help me figure out whether to put the application configuration  
below inside or outside of the Rails::Initializer in environment.rb.  
The application runs regardless of the position of the application  
configuration. However, a unit test fails if the application  
configuration is outside the block but will pass if the configuration is  
inside the block. Below are the code.

environment.rb:  
Code : - fold - unfold

1. RAILS\_GEM\_VERSION = ‘2.3.2’ unless defined? RAILS\_GEM\_VERSION
2. require File.join(File.dirname( **FILE** ), ‘boot’)
3. 
4. Rails::Initializer.run do |config|
5. 
6. config.action\_controller.session\_store = :active\_record\_store
7. 
8. config.gem ‘mislav-will\_paginate’, :version =\> ‘~\> 2.3.8’, :lib  
=\> ‘will\_paginate’,
9. :source =\> ‘[http://gems.github.com](http://gems.github.com)’
10. 
11. end
12. 
13. 
# Include your application configuration below
14. require ‘acts\_as\_ferret’
15. require ‘will\_paginate’
16. 
17. DB\_STRING\_MAX\_LENGTH = 255
18. DB\_TEXT\_MAX\_LENGTH = 40000
19. HTML\_TEXT\_FIELD\_SIZE = 15

test\_helper:  
Code : - fold - unfold

1. ENV[“RAILS\_ENV”] = “test”
2. require File.expand\_path(File.dirname( **FILE** ) +  
“/…/config/environment”)
3. require ‘test\_help’
4. 
5. class ActiveSupport::TestCase
6. …
7. end

faq\_test.rb:  
Code : - fold - unfold

1. require File.dirname( **FILE** ) + ‘/…/test\_helper’
2. 
3. class FaqTest \< ActiveSupport::TestCase
4. fixtures :faqs
5. 
6. def setup
7. 

```
@valid_faq = faqs(:valid_faq)

```

8. end
9. 
10. def test\_max\_lengths
11. 

```
Faq::QUESTIONS.each do |question|

```

12. 

```
  assert_length :max, @valid_faq, question, DB_TEXT_MAX_LENGTH

```

13. 

```
end

```

14. end
15. end

faq.rb:  
Code : - fold - unfold

1. class Faq \< ActiveRecord::Base
2. belongs\_to :user
3. acts\_as\_ferret
4. 
5. QUESTIONS = %w(bio skillz schools companies
6. 

```
             music movies television books magazines)

```

7. 
# A constant for everything except the bio
8. FAVORITES = QUESTIONS - %w(bio)
9. TEXT\_ROWS = 10
10. TEXT\_COLS = 40
11. 
12. validates\_length\_of QUESTIONS,
13. 

```
                  :maximum => DB_TEXT_MAX_LENGTH

```

14. 
15. def initialize
16. 

```
super

```

17. 

```
QUESTIONS.each do |question|

```

18. 

```
  self[question] = ""

```

19. 

```
end

```

20. end
21. end

When I run the unit test, with the application configuration outside the  
initializer block as above, I get the error that none of the variable  
outside the block is defined. If I move the “require” lines inside the  
block, the error will say that the constants are not defined, etc. (The  
line numbers in the error messages below may not be the same as the code  
line numbers above):

Code : - fold - unfold

1. ibookG4:rails\_space Beo$ rake test:units
2. (in /Users/Beo/Documents/Mesh/rails\_space)
3. /opt/local/bin/ruby -I"lib:test"  
“/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake\_test\_loader.rb”  
“test/unit/faq\_test.rb” “test/unit/helpers/community\_helper\_test.rb”  
“test/unit/helpers/faq\_helper\_test.rb”  
“test/unit/helpers/profile\_helper\_test.rb”  
“test/unit/helpers/site\_helper\_test.rb”  
“test/unit/helpers/spec\_helper\_test.rb”  
“test/unit/helpers/user\_helper\_test.rb” “test/unit/spec\_test.rb”  
“test/unit/user\_test.rb”
4. 

/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active\_record/base.rb:1964:in  
`method_missing_without_paginate': undefined local variable or method `acts\_as\_ferret’ for #Class:0x1a52430 (NameError)  
5. from  
/opt/local/lib/ruby/gems/1.8/gems/mislav-will\_paginate-2.3.11/lib/will\_paginate/finder.rb:170:in  
`method_missing' 6. from /Users/Beo/Documents/Mesh/rails_space/app/models/faq.rb:3 7. from /opt/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem\_original\_require’  
8. from  
/opt/local/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:31:in  
`require' 9. from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:158:in `require’  
10. from  
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active\_support/dependencies.rb:265:in  
`require_or_load' 11. from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:224:in `depend\_on’  
12. from  
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active\_support/dependencies.rb:136:in  
`require_dependency' 13. from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:393:in `load\_application\_classes’  
14. from  
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:392:in  
`each' 15. from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:392:in `load\_application\_classes’  
16. from  
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:390:in  
`each' 17. from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:390:in `load\_application\_classes’  
18. from  
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:195:in  
`process' 19. from /opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in `send’  
20. from  
/opt/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/initializer.rb:113:in  
`run' 21. from /Users/Beo/Documents/Mesh/rails_space/config/environment.rb:9 22. from ./test/unit/../test_helper.rb:2:in `require’  
23. from ./test/unit/…/test\_helper.rb:2  
24. from ./test/unit/faq\_test.rb:1:in `require' 25. from ./test/unit/faq_test.rb:1 26. from /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load’  
27. from  
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake\_test\_loader.rb:5  
28. from  
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake\_test\_loader.rb:5:in  
`each’  
29. from  
/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake\_test\_loader.rb:5  
30. rake aborted!  
31. Command failed with status (1): [/opt/local/bin/ruby -I"lib:test"  
"/opt/loc…]  
32.  
33. (See full trace by running task with --trace)

I am running the following gems. My plugins folder is empty:

Code : - fold - unfold

1. ibookG4:rails\_space Beo$ gem list
2. 
3. \*\*\* LOCAL GEMS \*\*\*
4. 
5. actionmailer (2.3.2)
6. actionpack (2.3.2)
7. activerecord (2.3.2)
8. activeresource (2.3.2)
9. activesupport (2.3.2)
10. acts\_as\_ferret (0.4.4)
11. ferret (0.11.6)
12. mislav-will\_paginate (2.3.11)
13. mysql (2.7)
14. rails (2.3.2)
15. rake (0.8.7)
16. rubygems-update (1.3.3)

You may say that I should just move them all inside the block and be  
done with it. After all, the application runs fine with those inside  
the block. However, according to the book “RailsSpace” that I am  
following, those app config should be outside the block. I’ve read also  
that the require lines for acts\_as\_ferret and will\_paginate should be  
outside the block. I am afraid that if I leave them inside the block, I  
may have some problems down the road. Please help. Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [June 26, 2009, 12:00am UTC](https://www.ruby-forum.com/t/inside-or-outside-of-rails-initializer-block/170986/2 "2009-06-26T00:00:25Z")

</div>

Matt,

Thank you very much for your valuable advices. I will definitely need  
to check out the latest good book on Rails. Which one would you  
recommend?

I also have discovered the wonder of plugins. Which plugins would you  
recommend for user authentication, file uploads and other common tasks  
for a social networking site?

I am trying to build a social networking site. That’s why the  
step-by-step instructions in RailsSpace are so convenient albeit sorely  
out-of-date. I have been spending quite a bit of time debugging and  
updating the code to Rails 2.3.2. Any suggestions would be greatly  
appreciated.

Thank you.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [June 25, 2009, 6:41pm UTC](https://www.ruby-forum.com/t/inside-or-outside-of-rails-initializer-block/170986/3 "2009-06-25T18:41:59Z")

</div>

The book is out of date. Seriously out of date, in fact. (built on  
Rails 1.2.3) So much has changed since then that I’d recommend getting  
almost _anything_ more modern.

You should replace the call to “require ‘acts\_as\_ferret’” to a  
config.gem statement, much like the one for mislav-will\_paginate that  
you’ve already got. You can also remove the “require ‘will\_paginate’”  
part, as the config.gem statement does it for you.

Finally, the current best practice for setting constants is to put  
them in a file in config/initializers.

Once again, I can’t over-stress the need to get a more modern  
reference for learning Rails. While the online source code has been  
somewhat updated, here’s a short list (took less than 5 minutes) of  
issues:

- views as .rhtml rather than .html.erb
- old-style tests in many places (derived from Test::Unit::TestCase,  
vs ActiveSupport::TestCase and friends); these will fail now
- spends a lot of time rolling its own authentication system; your  
time would be better spent picking a good plugin
- also rolls its own file upload
- very few named routes

–Matt J.

On Jun 24, 4:15 pm, Learn by Doing [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [June 26, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/inside-or-outside-of-rails-initializer-block/170986/4 "2009-06-26T22:28:40Z")

</div>

Thanks Matt for your prompt response and guidance.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [June 26, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/inside-or-outside-of-rails-initializer-block/170986/5 "2009-06-26T18:25:49Z")

</div>

For documentation, a good place to start is on the main Rails site -  
[http://rubyonrails.org/documentation](http://rubyonrails.org/documentation) . The Rails Guides can be a  
helpful source, and they are typically one of the most up-to-date  
places for info.

Book-wise, the discussion of “which book is the best” could fill a  
whole thread of its own. I got started with the first edition of Agile  
Web D. with Rails, so I’d lean towards recommending the  
current 3rd edition of that. Whatever you pick, try to avoid anything  
published before 2008, as Rails has been evolving rapidly lately.

As far as plugins, there’s simply too many different use cases to  
recommend any particular one over all the others. Github’s search  
function can sometimes help here. Whatever you find, pay attention to  
the commit history; nothing worse than getting stuck with a dead  
plugin.

–Matt J.

On Jun 25, 6:00 pm, Learn by Doing [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
