This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Do not connect to database if in Rails initialization stage #22
Comments
Which line of You can also use: Audited.config do |config|
if Audit.table_exists?
config.audit_class = ::Audit
end
end
###
class Audit
if Audit.table_exists?
attr_protected :customer_id
end
end |
Here is the stack trace:
|
Does the following monkey patch work for you? ActiveRecord::MassAssignmentSecurity::ClassMethods.module_eval do
def attributes_protected_by_default
if table_exists?
default = [ primary_key, inheritance_column ]
default << 'id' unless primary_key.eql? 'id'
default
else
[]
end
end
end |
It does not, because the call to
|
How about the following ActiveRecord::MassAssignmentSecurity::ClassMethods.module_eval do
def attributes_protected_by_default
begin
default = [ primary_key, inheritance_column ]
default << 'id' unless primary_key.eql? 'id'
default
rescue ActiveRecord::NoDatabaseError
[]
end
end
end |
Yes, that works. |
Ok this should now be fixed in master. |
v1.8.0 is now released which contains this fix. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
In Rails 5.2, a new behavior described here will cause Rails configs to be loaded for all rake tasks.
In our case, if the database has not yet been created (before
rake db:create
) has been run, and then we runrake db:create
, this is what happens:we are using the
audited
gem and using its initializer to configure the class to use for auditing:So that class is loaded, which has this code:
The
attr_protected
call connects to the database to determine the primary key of the model to use as a default id. But since the database has not yet been created, it fails. This creates a catch-22 where we cannot create the database, because the rake task to create it fails because there is no database.Ideally there would be a way to opt out of the behavior that by default connects to the database to determine the primary key.
The text was updated successfully, but these errors were encountered: