Rails / Troubleshooting

From WhyNotWiki
Jump to: navigation, search

Contents

[Caveats (category)] Some hashes are not of type HashWithIndifferentAccess

[To do: rewrite this betterly]

Implication: You can't do hash[:key] and expect it to work, if hash actually has a key of 'key'.

Many Some things in Rails (such as params and session) return hashes of class HashWithIndifferentAccess... which means you can access the key as either :key or 'key' (you don't have to care which is the actual key)...

But don't become so used to that behavior that you forget that normal hashes don't behave that way. In other words, any libraries (stuff you stick in lib/) you write, or pretty much anywhere you are constructing hashes like this: {key => value} -- it's going to just be a plain old Hash.

Here's a (somewhat boring) illustration:

> ./script/console
irb -> HashWithIndifferentAccess.new({:a=>2})['a']
    => 2

irb -> HashWithIndifferentAccess.new({:a=>2}).class
    => HashWithIndifferentAccess

# But!:

irb -> {:a=>2}.class
    => Hash

irb -> {:a=>2}['a']
    => nil

Question: So what actually uses HashWithIndifferentAccess rather than Hash?

Not session, apparently. Here's how I concluded that... [Dissections (category)] First of all, if we simply look at the value of session.class in our controller, we may be somewhat surprised to see CGI::Session rather than some sort of flavor of Hash. "Doesn't session behave as a hash though?!" you ask? Yes, it does. But only because it implements [] and []= (in other words, it acts like a Hash, thanks to duck typing): /usr/lib/ruby/1.8/cgi/session.rb

    # Retrieve the session data for key +key+.
    def [](key)
      @data ||= @dbman.restore
      @data[key]
    end

    # Set the session date for key +key+.
    def []=(key, val)
      @write_lock ||= true
      @data ||= @dbman.restore
      @data[key] = val
    end

Anyway, @dbman.restore will give us an instance of type CGI::Session::ActiveRecordStore... which is still not a hash, but getting closer...

I put the following debug output in /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/session/active_record_store.rb :

class CGI
  class Session
    class ActiveRecordStore
      class Session < ActiveRecord::Base
        ...
        # Lazy-unmarshal session state.
        def data
          @data ||= self.class.unmarshal(read_attribute(@@data_column_name)) || {}
          puts @data.class
          p @data
          @data
        end

Then, when I loaded the page, I saw this output from my development web server:

Hash
{"flash"=>{}, :user=>1509}

So the class is clearly Hash.

As further confirmation, I did this in my view:

<%= session[:user].inspect %>

and got this back as output: 1509.

<%= session['user'].inspect %>

and got this back as output: nil.

What about params? Putting this in our view:

<%= params.class %>

tells us the answer very quickly: HashWithIndifferentAccess. That was easy!

Question: So how/where does Rails actually convert things to HashWithIndifferentAccess??

cannot remove Object::__ / How to debug Dependencies problems

About my environment:


> ./script/about
About your application's environment
Ruby version                 1.8.5 (i386-linux)
RubyGems version             0.9.2
Rails version                1.2.3
Active Record version        1.15.3
Action Pack version          1.13.3
Action Web Service version   1.2.3
Action Mailer version        1.3.3
Active Support version       1.4.2
 

Here was the initial symptom in my case:

> rake remote:show_tasks --trace
rake aborted!
cannot remove Object::General
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:426:in `remove_const'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:426:in `send'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:426:in `remove_constant'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:372:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:372:in `each'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:372:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:501:in `require'
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/core/kernel/require_local.rb:13:in `require_local'
/home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/rails.rake:3
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:493:in `load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:493:in `load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:493:in `load'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8:in `each'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:501:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:501:in `require'
/home/tyler/svn/glass.net/frontend/old/Rakefile:10
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1929:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/bin/rake:7
/usr/bin/rake:16:in `load'
/usr/bin/rake:16

I tried enabling logging, like this (environment.rb):

RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
Dependencies::log_activity = true

, but it was too verbose in some ways (displayed stuff I wasn't interested in) and not verbose enough in other ways (didn't show enough context -- so all the log messages ended up looking the same):

Dependencies: called new_constants_in(Object)
Dependencies: New constants:
Dependencies: called new_constants_in(Object)
Dependencies: New constants:
Dependencies: called new_constants_in(Object)
Dependencies: New constants:
...


So I ended up putting some more-specialized debug output in /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:


  def load(file, *extras)
    $requiring ||= []                     # <--
    $requiring.push file                  # <--
    Dependencies.new_constants_in(Object) { super(file, *extras) }
  rescue Exception => exception  # errors from loading file
    exception.blame_file! file
    raise
  end

  def require(file, *extras)
    $requiring ||= []                     # <--
    $requiring.push file                  # <--
    Dependencies.new_constants_in(Object) { super(file, *extras) }
  rescue Exception => exception  # errors from required file
    exception.blame_file! file
    raise
  end



  def new_constants_in(*descs)
    aborting = true
    begin
      yield # Now yield to the code that is to define new constants.
      aborting = false
    ensure
      requiring = $requiring.pop          # <--

      # Find the new constants.
      new_constants = watch_frames.collect do |mod_name, prior_constants|
        ...
      end.flatten

      log "New constants: #{new_constants * ', '}"

      if aborting
        puts '{-----------------'
        puts "Error while attempting to load '#{$requiring}'. Removing partially loaded constants (#{new_constants.inspect})..."
        p $!
        log "Error during loading, removing partially loaded constants "
        new_constants.each { |name| puts "removing #{name}"; remove_constant name }
        new_constants.clear
        puts '}-----------------'
      end
    end
    ...

Which produced:


...
{-----------------
Error while attempting to load 'xsd/xmlparser/xmlscanner'. Removing partially loaded constants ([])...
#<MissingSourceFile: no such file to load -- xmlscan/scanner>
}-----------------
{-----------------
Error while attempting to load '/home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks'. Removing partially loaded constants (["ForApps", "SharedRailsTasks", "General", "Database", "ForPlugins"])...
ArgumentError: wrong number of arguments (1 for 0)
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:49:in `namespace'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:49:in `included'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:36:in `class_eval'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:36:in `included'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:18:in `include'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:18:in `included'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:324:in `include'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/shared_rails_tasks.rb:324
  /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
  /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
  /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/core/kernel/require_local.rb:13:in `require_local'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/rails.rake:6
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
  /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
  /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8:in `each'
  /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
  /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
  /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
  /home/tyler/svn/glass.net/frontend/old/Rakefile:10
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load'
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load_rakefile'
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1929:in `run'
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/bin/rake:7
  /usr/bin/rake:16:in `load'
  /usr/bin/rake:16
removing ForApps
{-----------------
Error while attempting to load '/home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/rails.rake'. Removing partially loaded constants ([])...
NameError: cannot remove Object::ForApps
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `remove_const'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `send'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `remove_constant'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `new_constants_in'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `each'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `new_constants_in'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
  /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/core/kernel/require_local.rb:13:in `require_local'
  /home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/rails.rake:6
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
  /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
  /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8:in `each'
  /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
  /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
  /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
  /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
  /home/tyler/svn/glass.net/frontend/old/Rakefile:10
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load'
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load_rakefile'
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1929:in `run'
  /usr/lib/ruby/gems/1.8/gems/rake-0.7.2/bin/rake:7
  /usr/bin/rake:16:in `load'
  /usr/bin/rake:16
}-----------------
...
...
rake aborted!
cannot remove Object::ForApps
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `remove_const'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `send'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `remove_constant'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `each'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/core/kernel/require_local.rb:13:in `require_local'
/home/tyler/svn/glass.net/frontend/old/config/../vendor/plugins/shared_rails_tasks/tasks/rails.rake:6
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8:in `each'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
/home/tyler/svn/glass.net/frontend/old/Rakefile:10
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1929:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/bin/rake:7
/usr/bin/rake:16:in `load'
/usr/bin/rake:16
 

The source of the problem / solution

The Module#namespace method I'd written in quality_extensions (quality_extensions/module/namespace) was conflicting with Rake's namespace method. So when I was calling namespace from my Rakefile, thinking that I was calling a method from the Rake library, I really wasn't -- I was calling a completely different method that "just happened" to have the same name...

Well, my solution in this case was to rename my namespace to something else (namespace_module) (fixed in quality_extensions-0.0.52), but I wish I didn't have to do that. Can't our two libraries just get along? I guess that's just one of the dangers of adding methods to important base classes like Module...

Other questions

This raises several other questions, though:

  • Why did it think General was a constant in Object when it's actually a constant in the SharedRailsTasks module?
  • Why did all those other files ...

utf8proc_native, db2/db2cli, oci8, oci8, sybsql, Win32API, memcache, redcloth, redcloth, bluecloth, bluecloth, html/tokenizer, tmail/base64.so, tmail/scanner_c.so, xml/parser, xml/parser, xmlscan/scanner, xmlscan/scanner ) fail to load? ...Not that it's ever caused this problem, where it caused it to try to remove constants and then the constant removal failed... It's probably been failing on these files before for a long time and I'd just never noticed it before because it fails silently and it's not important that it not fail... .

Why did it think ForApps was a constant in Object when it's actually a constant in the SharedRailsTasks module?

ForApps is clearly a constant within SharedRailsTasks, not within Object!

p SharedRailsTasks.constants
# => ["Database", "ForPlugins", "General", "ForApps"]

To answer that question, I pared down my Rakefile to just the minimum needed to reproduce this behavior...

Deleted all plugins. Created a new dummy plugin and moved my rake task code into it...

module MyModule
  def self.included(base_module)
    include MyModule::General
  end

  # The tasks defined in this module can be used by both apps and plugins.
  module General
    def self.included(base_module)
      raise 'An error'
    end
  end
end

include MyModule

I tried moving the contents of my plugin's rake file into the main Rakefile, but that was not sufficient to reproduce this behavior. It caused an error to be raised (of course):

rake aborted!
An error
/home/tyler/svn/glass.net/frontend/old/Rakefile:26:in `included'
/home/tyler/svn/glass.net/frontend/old/Rakefile:20:in `include'
/home/tyler/svn/glass.net/frontend/old/Rakefile:20:in `included'
/home/tyler/svn/glass.net/frontend/old/Rakefile:32:in `include'
/home/tyler/svn/glass.net/frontend/old/Rakefile:32
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1855:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake.rb:1929:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/bin/rake:7
/usr/bin/rake:16:in `load'
/usr/bin/rake:16

... but not this one, which is the error I get when that same code is moved to vendor/plugins/dummy/tasks/dummy_tasks.rake:

cannot remove Object::General
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `remove_const'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `send'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:428:in `remove_constant'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `each'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:373:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:496:in `load'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8:in `each'
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:8
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:505:in `require'
/home/tyler/svn/glass.net/frontend/old/Rakefile:9

In other words, that error only occurs when my rake tasks are loaded through this mechanism in /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/tasks/rails.rb:

Dir["#{RAILS_ROOT}/vendor/plugins/**/tasks/**/*.rake"].sort.each { |ext| load ext }

Well, I dug around in /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb a bit more, trying to figure out why it wasn't working as expected, but I couldn't really figure it out...

It looks like this is where it got Object from...

    Dependencies.new_constants_in(Object) { super(file, *extras) }

so I'm not really sure how it's supposed to work. Oh well, I won't waste any more time on this one...


Application error (failed to start)

Check the log file.

Reasons for this error:

  • file script/../config/../tmp/sessions//ruby_sess.0f36e74cd2a06698 not readable (solution: delete cookie to force new session)
  • permissions
  • failed to include some required file
  • (production:) pretty much any exception

[ActiveRecord (category)]::ConnectionNotEstablished : may simply be the permissions on production.log!

At least that's how I fixed it in this case (2007-01-16):

$ ./script/console production
Loading production environment.
/usr/lib/ruby/1.8/logger.rb:527:in `initialize':Errno::EACCES: Permission denied - script/../config/../config/../log/production.log
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in `const_missing':NameError: uninitialized constant ExceptionNotifiable
>> User.find(:first)
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:225:in `retrieve_connection'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1046:in `add_limit!'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1017:in `construct_finder_sql'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:924:in `find_every'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:918:in `find_initial'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:380:in `find'
        from (irb):1

Tried this, but I didn't have permissions to modify the file:

$ chmod a+w log/production.log
chmod: changing permissions of `log/production.log': Operation not permitted

But I do have drwxrwxrwx on the log directory, so that must be sufficient permissions to move the file:

$ mv log/production.log log/production.log.20070116; touch log/production.log

(The already-running application continued to append to the old file (log/production.log.20070116). However, when I ran script/console production, the console logged to the new file (log/production.log).)

When I tried it this time, it worked!

$ ./script/console production
Loading production environment.
>> User.find(:first)
=> #<User:0xb7460604 ...>

Caveat: `alias_method': undefined method `[...]_with_deprecation' error

This is the result an [incompatibility (category)] between Rails' version of alias_method_chain and Facets' version.

Example of error:

/usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/core/module/alias_method_chain.rb:34:in `alias_method': undefined method `add_benchmarks_with_deprecation' for class `AppProfilerRequest' (NameError)

Current as of: Facets 1.8.54

[Workaround (category)]:

Method 1, which I think only works with 1.2.3

config/environment.rb:


# Make sure this gets added to $LOADED_FEATURES now so that it won't get loaded again.
require 'facets/core/module/alias_method_chain'  

# Override alias_method_chain (again!) with the Rails version (which is compatible with the Facets version)
load 'active_support/core_ext/module/aliasing.rb'   # This file only exists in Rails 1.2.3

# That should all be above the Initializer code in case one of your plugins [...]...

Rails::Initializer.run do |config|
  ...
end

Method 2

Another method of doing this (courtesy of Lance):

require File.join(File.dirname(__FILE__), 'boot')
# By this point, it appears that ActiveSupport has been loaded, so Rails's version of Module.alias_method_chain has already been defined.

saved_alias_method_chain = Module.method(:alias_method_chain)

Rails::Initializer.run do |config|
  ...
end

# Make sure this gets added to $LOADED_FEATURES now so that it won't get loaded again.
require 'facets/core/module/alias_method_chain'  

# Override alias_method_chain (again!) with the Rails version (which is compatible with the Facets version)
Module.send(:define_method, :alias_method_chain, saved_alias_method_chain)

Variations

Sometimes neither of those methods work. Sometimes you have to do the saving of the Rails method or the restoring thereof sooner or later...

I had a similar issue although the fix was a little different than the previous examples. I instead had to require the alias_method_chain and load the rails aliasing at the bottom of the environment.rb (Rails 1.2.3). The second fix also didn't work erroring with:
config/environment.rb:12:in `method': undefined method `alias_method_chain' for class `Class' (NameError)

Example:
config/environment.rb:

Rails::Initializer.run do |config|
  ...
end

require 'facets/core/module/alias_method_chain'  
load 'active_support/core_ext/module/aliasing.rb'

One time, I couldn't get any of these methods to work, so I copied the aliasing.rb method from Rails 1.2.3, threw it in lib/ and loaded that from my Rails 1.1.6 app (because I can't find 'def alias_method_chain' anywhere in the Rails 1.1.6 code!)...

Ticket

Ticket: http://rubyforge.org/tracker/index.php?func=detail&aid=9791&group_id=804&atid=3169

Caveat!: Why am I getting a "wrong number of arguments (2 for 0)" error?

One possible reason: you have a column named 'quote'.

http://rails.techno-weenie.net/question/2006/8/10/wrong-number-of-arguments-issue:

Make sure you don't have a column called "quote" in any of your tables. This will override the current ActiveRecord "quote" method. http://dev.rubyonrails.org/ticket/3628

[Minor (category)][Caveats (category)]: If you modify log/development.log while your development web server is running and then make a request, nothing will get logged.

[Is this reliably reproducible?]

Solution: restart the web server and try again.

Or don't modify the log while your server is running...

Ads
Personal tools