Skip to content

Precompile tries to access the database on acceptance validation #11381

Closed
willc0de4food opened this Issue Jul 9, 2013 · 23 comments

7 participants

@willc0de4food

I have a validation that checks to make sure a checkbox is checked. When I try to precompile assets, it's hitting that validation, attempting to access the database, and failing (I'm deploying this app to Heroku, so this isn't going to work). This seems to be the culprit: https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/attribute_methods.rb#L99
I do have config.assets.initialize_on_precompile = false in my application.rb. Here's a gist of the code I'm referencing: https://gist.github.com/willc0de4food/ca3c78f149dc5c425dcd
I also attempted to wrap the validation in ActiveSupport.on_load, with the same problem: https://gist.github.com/willc0de4food/3c48c5e4a2efb938b3b1

@pftg
pftg commented Jul 9, 2013

Do you have any initializers, which require Contract model?
Also as I understand you use rails 4.0.0_, if I'm right you do not need config.assets.initialize_on_precompile = false.

@willc0de4food

Good to know, thanks.
The only initializer I have that might try, is the rails_admin initializer, but I'm telling it to blacklist the contract model: https://gist.github.com/willc0de4food/786b86ae8bc8bbe27eb9

@pftg
pftg commented Jul 9, 2013

hmm, elsewhere, some from your engines require 'Contractor' model on environment loading. I blame on rails_admin, but cannot say for sure. If you have complete rails examples, because by info which you have provided I cannot reproduce your problem. Default generated rails app with rails admin do not load any models.

@steveklabnik
Ruby on Rails member

Rails (like all of us 😉) is innocent until proven guilty. As @pftg mentions, it doesn't appear that this is Rails' issue. If you can reproduce this with vanilla Rails, please show that to us and we can re-open, but until then, this isn't a bug.

@willc0de4food

https://github.com/willc0de4food/checkbox_acceptance_test

That's a pretty bare-bones app, and RAILS_ENV=production bundle exec rake assets:precompile is failing with the same error.

@pftg
pftg commented Jul 9, 2013

@willc0de4food I think you have point. I'm investigating why in your app acceptance validator breaks rake assets:precompile

@pftg
pftg commented Jul 9, 2013

just for notice, this only happens in your app.

@willc0de4food

Weird. Lol, so my Rails is the only one that's broken :[ Oh well. Just as an FYI, I can recreate this scenario every time on my machine: https://github.com/willc0de4food/checkbox_acceptance_test2

@pftg
pftg commented Jul 9, 2013

I created test case, so all models inherited from ActiveRecord::Base preloaded. So this is problem.

@mitchkett

I can recreate after cloning his repo, cd into directory, run bundle install, then RAILS_ENV=production bundle exec rake assets:precompile

rake aborted!
Unknown database 'checkbox_test2_production'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in connect'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in
initialize'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in new'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in
mysql2_connection'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in new_connection'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in
checkout_new_connection'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in acquire_connection'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in
block in checkout'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in checkout'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in
block in connection'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in connection'
/Users/mitchkett/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in
retrieve_connection'
...

@mitchkett

The error does not get thrown if you run the rake command for development or test, just production.

@pftg
pftg commented Jul 9, 2013

I think, because eager_load! automatically invoked for production.

@pftg
pftg commented Jul 9, 2013

For now added fixed tests. Will continue investigation on eager loaded modules for production env.

@miry
miry commented Jul 9, 2013

Try to set config.assets.initialize_on_precompile = false in config/application.rb. http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

@miry
miry commented Jul 9, 2013

@willc0de4food try this patch #11384

@willc0de4food

Are there instructions somewhere on the process of apply such a patch?

@miry
miry commented Jul 10, 2013

@willc0de4food replace config.eager_load = true with config.eager_load = true if config.eager_load.nil?

@pftg
pftg commented Jul 10, 2013

I added PR #11389 with fixing bug for rake and fixing tests.

@willc0de4food

@miry that worked. No more error. I didn't realize you meant in the app's environment file :X

@pftg pftg added a commit to jetthoughts/rails that referenced this issue Jul 10, 2013
@pftg pftg #11381: Ignore config.eager_load=true for rake
Closes #11381
9cac69c
@guilleiguaran
Ruby on Rails member

config.assets.initialize_on_precompile isn't supported in Rails 4 anymore 😁

@gugl
gugl commented Jul 22, 2014

looks like there is a reference to #11853
i also added a way to reproduce the issue. https://github.com/gugl/virtattr
any ideas for solutions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.