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.

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. Rails::Initializer.run do |config|
  4. config.action_controller.session_store = :active_record_store
  5. config.gem ‘mislav-will_paginate’, :version => ‘~> 2.3.8’, :lib
    => ‘will_paginate’,
  6. :source => ‘http://gems.github.com
  7. end
  8. Include your application configuration below

  9. require ‘acts_as_ferret’
  10. require ‘will_paginate’
  11. DB_STRING_MAX_LENGTH = 255
  12. DB_TEXT_MAX_LENGTH = 40000
  13. 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. class ActiveSupport::TestCase
  5. end

faq_test.rb:
Code : - fold - unfold

  1. require File.dirname(FILE) + ‘/…/test_helper’
  2. class FaqTest < ActiveSupport::TestCase
  3. fixtures :faqs
  4. def setup
  5. @valid_faq = faqs(:valid_faq)
    
  6. end
  7. def test_max_lengths
  8. Faq::QUESTIONS.each do |question|
    
  9.   assert_length :max, @valid_faq, question, DB_TEXT_MAX_LENGTH
    
  10. end
    
  11. end
  12. end

faq.rb:
Code : - fold - unfold

  1. class Faq < ActiveRecord::Base
  2. belongs_to :user
  3. acts_as_ferret
  4. QUESTIONS = %w(bio skillz schools companies
  5.              music movies television books magazines)
    
  6. A constant for everything except the bio

  7. FAVORITES = QUESTIONS - %w(bio)
  8. TEXT_ROWS = 10
  9. TEXT_COLS = 40
  10. validates_length_of QUESTIONS,
  11.                   :maximum => DB_TEXT_MAX_LENGTH
    
  12. def initialize
  13. super
    
  14. QUESTIONS.each do |question|
    
  15.   self[question] = ""
    
  16. end
    
  17. end
  18. 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”

/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. *** LOCAL GEMS ***
  3. actionmailer (2.3.2)
  4. actionpack (2.3.2)
  5. activerecord (2.3.2)
  6. activeresource (2.3.2)
  7. activesupport (2.3.2)
  8. acts_as_ferret (0.4.4)
  9. ferret (0.11.6)
  10. mislav-will_paginate (2.3.11)
  11. mysql (2.7)
  12. rails (2.3.2)
  13. rake (0.8.7)
  14. 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.

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.

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 [email protected]

Thanks Matt for your prompt response and guidance.

For documentation, a good place to start is on the main Rails site -
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 [email protected]