Closed phil-monroe closed 9 years ago
And just for anyone that is interested, I'm currently working around the problem by using this monkeypatch that should be removed when the problem is fixed. If you use it, be sure to hardcode the proper class for your "user" model.
# confing/initializers/clearance.rb
# TODO: remove this monkeypatch when clearance is fixed
# https://github.com/thoughtbot/clearance/issues/568
module Clearance
class Configuration
def user_model
::User
end
end
end
Clearance.configure do |config|
#...
end
@phil-monroe Are you using Clearance 1.9.0 or newer? We added this change to try to address these issues.
Yup. Was using 1.8.0. Thanks!
When using clearance, we keep getting an
ActiveRecord::AssociationTypeMismatch
whenever Rails auto-reloads the user class and we try to setcurrent_user
as an association for a new record. I think the issue is stemming from this line:https://github.com/thoughtbot/clearance/blob/master/lib/clearance/configuration.rb#L103
This is storing a reference to the class of the User model as an instance variable, which won't get updated when rails auto-reloads the class. I've been able to verify this by using
pry
to debug at the beginning of a request.When trying to set the current user as an association on a model, an
ActiveRecord::AssociationTypeMismatch
is thrown.I can think of a couple ways of solving this, but would like the maintainers feedback before proceeding. Some possible options are:
1) Convert the class name to string and constantize (
@user_model.name.constantize
) to get the latest version of the class whenever accessing the user model. I feel like this is kind of a hack and a bit wastful from an memory usage standpoint, but I think I've seen it done elsewhere.2) Allow
user_model
to be set to something callable that returns a user model. Would look something like this:3) Add a
before_action
toClearance::Controller
to check ifClearance.configuration.user_model
is the latest version and update if need be. Would be kind of nice as you can skip the before_action ifRails.configuration.cache_classes
is set to true.4) Kind of like 3, but add a cleanup hook to
ActionDispatch::Reloader
to reset theuser _model
.Let me know what you all think and I'd be glad to supply a pull request and/or an example Rails app.