Inside or outside of Rails::Initializer block

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.

Code : - fold - unfold

   1. RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
   2. require File.join(File.dirname(__FILE__), 'boot')
   4. do |config|
   6. config.action_controller.session_store = :active_record_store
   8. config.gem 'mislav-will_paginate', :version => '~> 2.3.8', :lib
=> 'will_paginate',
   9. :source => ''
  11. end
  13. # Include your application configuration below
  14. require 'acts_as_ferret'
  15. require 'will_paginate'
  18. DB_TEXT_MAX_LENGTH = 40000

Code : - fold - unfold

   1. ENV["RAILS_ENV"] = "test"
   2. require File.expand_path(File.dirname(__FILE__) +
   3. require 'test_help'
   5. class ActiveSupport::TestCase
   6. ...
   7. end

Code : - fold - unfold

   1. require File.dirname(__FILE__) + '/../test_helper'
   3. class FaqTest < ActiveSupport::TestCase
   4. fixtures :faqs
   6. def setup
   7. @valid_faq = faqs(:valid_faq)
   8. end
  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

Code : - fold - unfold

   1. class Faq < ActiveRecord::Base
   2. belongs_to :user
   3. acts_as_ferret
   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
  12. validates_length_of QUESTIONS,
  13. :maximum => DB_TEXT_MAX_LENGTH
  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"
"test/unit/faq_test.rb" "test/unit/helpers/community_helper_test.rb"
"test/unit/helpers/user_helper_test.rb" "test/unit/spec_test.rb"
`method_missing_without_paginate': undefined local variable or method
`acts_as_ferret' for #<Class:0x1a52430> (NameError)
   5. from
   6. from /Users/Beo/Documents/Mesh/rails_space/app/models/faq.rb:3
   7. from
   8. from
   9. from
  10. from
  11. from
  12. from
  13. from
  14. from
  15. from
  16. from
  17. from
  18. from
  19. from
  20. from
  21. from
  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
  27. from
  28. from
  29. from
  30. rake aborted!
  31. Command failed with status (1): [/opt/local/bin/ruby -I"lib:test"
  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
   3. *** LOCAL GEMS ***
   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.

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

- 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 Jones


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

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

Thank you.

For documentation, a good place to start is on the main Rails site - . 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 Development 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

--Matt Jones