assets:precompile tries to establish a database connection
When this gem is in the Gemfile, it is included whenever the rails environment is loaded since it is in the default group. Since it touches the db on load, it breaks assets:precompile when no database is configured.
A workaround is to use
gem 'activerecord-session_store', require: false
in the Gemfile and then
require 'activerecord/session_store'
in app/controllsers/application_controller.rb. Also, it took me a lot of time to determine which gem was responsible for the db connection. This is how I ended up "debugging" it: I edited active_record/lib/active_record/base.rb and added "puts caller" to the top of the file. The stacktrace would then show which gem was loading it.
I'm not convinced this is the fault of activerecord-session_store. This gem creates a subclass of ActiveRecord::Base as a model to be used to store the session, and it's ActiveRecord::Base that tries to load your database configuration.
As it turns out, assets:precompile does not try to establish a database connection. It's just loading your database configuration. Because of this, there is a simpler way to work around this issue. Just provide a dummy configuration for the environment you're trying to run rake assets:precompile in. For example:
development:
adapter: sqlite3
database: fake_database
Granted, perhaps the issue you're seeing could be fixed/avoided in ActiveRecord::Base, but I haven't looked that far yet. At the least, I don't think it's the fault of activerecord-session_store.
Thanks a lot for taking the time to look into this. Unfortunately, using sqlite3 as you suggest, would require the sqlite gem to be installed as well as the system libraries for sqlite. I would like to avoid that if possible: We are building docker images and I'd like to keep them as small as possible.
The way I understand it is that loading the database configuration is done via a hook by ActiveSupport (ActiveSupport.on_ load 'active_record' do ... or similar). activerecord-session_store indeed just subclasses ActiveRecord::Base but that loads ActiveRecord and triggers the hook. Without the session store, ActiveRecord is not loaded and therefore the hook code never runs. A "solution" I could see is that activerecord-session_store only loads when the rack application is actually started but that might not be feasable.
The problem mainly became foreground for me because Rails removed the "assets" group from the Gemfile and Rails.groups always includes the "default" group where activerecord-session_store is in (so its always loaded).
A more generic workaround is to do something like:
if !ENV['RAILS_GROUPS'] || !ENV['RAILS_GROUPS'].match(/assets/)
gem 'activerecord-session_store'#, require: false
gem 'delayed_job_active_record'
gem 'awesome_nested_set', "~> 3.0.0"
gem 'factory_girl_rails'
end
within the Gemfile. Not pretty, but it works. Here, you can see that also other gems load ActiveRecord. Feel free to close the issue as a wontfix.
I meet this error too! I workaround this by checking file config/database.yml is exist.
# config/initializers/session_store.rb
if File.exist?(File.join(Rails.root, 'config/database.yml'))
ActiveRecord::SessionStore::Session.table_name = :sessions
ActiveRecord::SessionStore::Session.serializer = :null
Rails.application.config.session_store :active_record_store, key: 'mf_'
# I am setting a job to clean outdate session, so I must initialize here when server start
SessionCleanJob.enqueue
end
So far it work just fine. maby add gem 'activerecord-session_store', require: false to Gemfile and add require 'activerecord/session_store' to config/initializers/session_store.rb will be better.