sparkfly / foreigner

Foreigner -- Foreign Key Constraints for PostgreSQL and MySQL. *Thoroughly* tested via rspec.
MIT License
9 stars 2 forks source link

Error on startup #3

Closed wjessop closed 13 years ago

wjessop commented 13 years ago

Using Rails 3.0.3 and the latest gem version of foreigner I get this error when rails tries to start up:

(master) pea:sendl will$ rails g migration AddForeignKeys
/Users/will/.rvm/gems/ruby-1.9.2-p0/gems/sparkfly-foreigner-0.7.0/lib/foreigner.rb:29:in `block in on_load': undefined method `config' for nil:NilClass (NoMethodError)
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/sparkfly-foreigner-0.7.0/lib/foreigner.rb:27:in `on_load'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/sparkfly-foreigner-0.7.0/lib/foreigner.rb:45:in `'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.12/lib/bundler/runtime.rb:68:in `require'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.12/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.12/lib/bundler/runtime.rb:66:in `each'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.12/lib/bundler/runtime.rb:66:in `block in require'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.12/lib/bundler/runtime.rb:55:in `each'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.12/lib/bundler/runtime.rb:55:in `require'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/bundler-1.0.12/lib/bundler.rb:120:in `require'
    from /Users/will/Dropbox/www/sendl/config/application.rb:7:in `'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:15:in `require'
    from /Users/will/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:15:in `'
    from script/rails:6:in `require'
    from script/rails:6:in `
'

foreigner.rb:29 is:

ActiveRecord::Base.configurations = Rails.application.config.database_configuration

If I remove foreigner from the Gemfile and load up the console the config method is available just fine:

ruby-1.9.2-p0 > Rails.application.config.database_configuration
 => {"development"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"sendl_development", "pool"=>5, "username"=>"will", "password"=>nil}, "test"=>{"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"sendl_test", "pool"=>5, "username"=>"will", "password"=>nil}} 
hosh commented 13 years ago

This one issue with trying the load database_configuration is the source of the biggest incompatibilities of Foreigner with a a number of gems. The real fix is being able to hook into the ActiveRecord initialization stage and insert code, something like an after_establish_connection :load_foreigner ... or something to that effect. There's a Rails issue tracked with this. I've also thought about monkey-patching ActiveRecord.

This might be a 1.9 issue too. I have a 1.9 branch setup for the app I'm using this with, though I don't remember if the on_load / config is nil issue shows up. I won't be working on the 1.9 stuff anytime soon, though.

wjessop commented 13 years ago

FWIW I got it working fine with https://github.com/matthuhiggins/foreigner, so it seems like its something specific to this fork that is causing the issue.

edwintorok commented 13 years ago

This fork seems to have a fix: https://github.com/theaboutbox/foreigner/commit/062955abe913f9dd0f268988c282773315a0bc79

hosh commented 13 years ago

Thanks edwintorok, I'll get another release out.

hosh commented 13 years ago

@edwintorok: thanks for pointing out the commit. https://github.com/hosh/foreigner/tree/v0.7.1 and pushed to gemcutter.