New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Could not find a valid mapping" Error on Update #16

Closed
jamilabreu opened this Issue Apr 20, 2015 · 17 comments

Comments

Projects
None yet
5 participants
@jamilabreu
Contributor

jamilabreu commented Apr 20, 2015

When updating a person in my Person model on a clean rails app, I'm getting the following error:

Could not find a valid mapping for #<Person created_at:...

The changes get saved, but the error comes up. Here's part of the trace:

devise (3.4.1) lib/devise/mapping.rb:43:in `find_scope!'
devise (3.4.1) lib/devise/controllers/helpers.rb:166:in `signed_in_root_path'
devise (3.4.1) app/controllers/devise/registrations_controller.rb:132:in `after_update_path_for'
devise (3.4.1) app/controllers/devise/registrations_controller.rb:62:in `update'
actionpack (4.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.1) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.1) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'

Update: only getting this error when attempting to update via Devise::RegistrationsController#update

@andrewhavens

This comment has been minimized.

Show comment
Hide comment
@andrewhavens

andrewhavens Jul 2, 2015

Contributor

I am also running into this issue. I've implemented a custom token auth which calls the devise sign_in helper:

def authenticate_user_from_token!
    authenticate_with_http_token do |token, options|
      user = User.find_by(email: options[:email])
      if user && Devise.secure_compare(user.authentication_token, token)
        sign_in user, store: false
      end
    end
  end

It's failing with the same message because the sign_in helper is also calling find_scope!. In my case, I've traced it down to this line:

Devise.mappings.each_value { |m| return m.name if obj.is_a?(m.to) }

The comparison of obj.is_a?(m.to) is failing because obj is a User, but m.to is returning:

User(authentication_token: Object, confirmation_sent_at: DateTime, confirmation_token: Object, confirmed_at: DateTime, created_at: DateTime, current_sign_in_at: DateTime, current_sign_in_ip: Object, email: Object, encrypted_password: Object, facebook_token: Object, first_name: Object, last_name: Object, last_sign_in_at: DateTime, last_sign_in_ip: Object, remember_created_at: DateTime, reset_password_sent_at: DateTime, reset_password_token: Object, sign_in_count: Object, tos: Object, unconfirmed_email: Object, updated_at: DateTime)

I traced this down to find why m.to was returning this. m is an instance of a Devise::Mapping. The #to method simply calls @klass.get. When the mapping is initialized, @klass is set to:

@class_name = User # in my case
@klass = Devise.ref(@class_name)

When Devise.ref is executed, it calls this code, which I think is the core issue:

  def self.ref(arg)
    if defined?(ActiveSupport::Dependencies::ClassCache)
      ActiveSupport::Dependencies::reference(arg)
      Getter.new(arg)
    else
      ActiveSupport::Dependencies.ref(arg)
    end
  end

...which then calls Devise::Getter.new(User) which results in:

#<Devise::Getter:0x007f98d95c0ef8 @name=User(authentication_token: Object, confirmation_sent_at: DateTime, confirmation_token: Object, confirmed_at: DateTime, created_at: DateTime, current_sign_in_at: DateTime, current_sign_in_ip: Object, email: Object, encrypted_password: Object, facebook_token: Object, first_name: Object, last_name: Object, last_sign_in_at: DateTime, last_sign_in_ip: Object, remember_created_at: DateTime, reset_password_sent_at: DateTime, reset_password_token: Object, sign_in_count: Object, tos: Object, unconfirmed_email: Object, updated_at: DateTime)>

I don't know the solution to this yet. I'm wondering if it has anything to do with class caching. I just upgraded to v5 of the neo4j gem. I don't think I remember seeing this before.

I'm also running the latest version of devise (3.5.1).

Contributor

andrewhavens commented Jul 2, 2015

I am also running into this issue. I've implemented a custom token auth which calls the devise sign_in helper:

def authenticate_user_from_token!
    authenticate_with_http_token do |token, options|
      user = User.find_by(email: options[:email])
      if user && Devise.secure_compare(user.authentication_token, token)
        sign_in user, store: false
      end
    end
  end

It's failing with the same message because the sign_in helper is also calling find_scope!. In my case, I've traced it down to this line:

Devise.mappings.each_value { |m| return m.name if obj.is_a?(m.to) }

The comparison of obj.is_a?(m.to) is failing because obj is a User, but m.to is returning:

User(authentication_token: Object, confirmation_sent_at: DateTime, confirmation_token: Object, confirmed_at: DateTime, created_at: DateTime, current_sign_in_at: DateTime, current_sign_in_ip: Object, email: Object, encrypted_password: Object, facebook_token: Object, first_name: Object, last_name: Object, last_sign_in_at: DateTime, last_sign_in_ip: Object, remember_created_at: DateTime, reset_password_sent_at: DateTime, reset_password_token: Object, sign_in_count: Object, tos: Object, unconfirmed_email: Object, updated_at: DateTime)

I traced this down to find why m.to was returning this. m is an instance of a Devise::Mapping. The #to method simply calls @klass.get. When the mapping is initialized, @klass is set to:

@class_name = User # in my case
@klass = Devise.ref(@class_name)

When Devise.ref is executed, it calls this code, which I think is the core issue:

  def self.ref(arg)
    if defined?(ActiveSupport::Dependencies::ClassCache)
      ActiveSupport::Dependencies::reference(arg)
      Getter.new(arg)
    else
      ActiveSupport::Dependencies.ref(arg)
    end
  end

...which then calls Devise::Getter.new(User) which results in:

#<Devise::Getter:0x007f98d95c0ef8 @name=User(authentication_token: Object, confirmation_sent_at: DateTime, confirmation_token: Object, confirmed_at: DateTime, created_at: DateTime, current_sign_in_at: DateTime, current_sign_in_ip: Object, email: Object, encrypted_password: Object, facebook_token: Object, first_name: Object, last_name: Object, last_sign_in_at: DateTime, last_sign_in_ip: Object, remember_created_at: DateTime, reset_password_sent_at: DateTime, reset_password_token: Object, sign_in_count: Object, tos: Object, unconfirmed_email: Object, updated_at: DateTime)>

I don't know the solution to this yet. I'm wondering if it has anything to do with class caching. I just upgraded to v5 of the neo4j gem. I don't think I remember seeing this before.

I'm also running the latest version of devise (3.5.1).

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 2, 2015

Member

We solved neo4jrb/neo4j#717 the other day, which had very similar symptoms: an object was given to a method that checked its type but it claimed the type did not match, even when it should have.

In that case, it was the result of class caching. I worked around it by getting rid of the is_a? call and using other qualities of the models, but I didn't spend a lot of time trying to get to the root cause. I wonder if https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_node/labels.rb#L8 is contributing to it. It might be hanging onto old versions of classes after the Rails reloader has instantiated new ones.

Can't say anything about Devise::Getter but the similarities between problems is suspicious. The line linked above is the first thing I think of when "class caching" comes up, so if there is some conflict, that's probably a good place to start.

Member

subvertallchris commented Jul 2, 2015

We solved neo4jrb/neo4j#717 the other day, which had very similar symptoms: an object was given to a method that checked its type but it claimed the type did not match, even when it should have.

In that case, it was the result of class caching. I worked around it by getting rid of the is_a? call and using other qualities of the models, but I didn't spend a lot of time trying to get to the root cause. I wonder if https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/active_node/labels.rb#L8 is contributing to it. It might be hanging onto old versions of classes after the Rails reloader has instantiated new ones.

Can't say anything about Devise::Getter but the similarities between problems is suspicious. The line linked above is the first thing I think of when "class caching" comes up, so if there is some conflict, that's probably a good place to start.

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 3, 2015

Member

If one of you guys have a chance, can you see if neo4jrb/neo4j#855 fixes it? Just gem 'neo4j', github: 'neo4jrb/neo4j', branch: 'constantize_problem' and bundle.

Member

subvertallchris commented Jul 3, 2015

If one of you guys have a chance, can you see if neo4jrb/neo4j#855 fixes it? Just gem 'neo4j', github: 'neo4jrb/neo4j', branch: 'constantize_problem' and bundle.

@andrewhavens

This comment has been minimized.

Show comment
Hide comment
@andrewhavens

andrewhavens Jul 3, 2015

Contributor

@subvertallchris This might have worked. I waited until I started seeing the errors again, updated my Gemfile to your branch, bundled, then restarted the app and the errors went away. However, restarting the app might have fixed this as well. I'll keep running your branch over the next few days and see if the error comes back.

Contributor

andrewhavens commented Jul 3, 2015

@subvertallchris This might have worked. I waited until I started seeing the errors again, updated my Gemfile to your branch, bundled, then restarted the app and the errors went away. However, restarting the app might have fixed this as well. I'll keep running your branch over the next few days and see if the error comes back.

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 3, 2015

Member

If it's the same root cause, the restart will always clear up the
temporarily. Hopefully, this does it permantnetly, but even if it doesn't,
I think we'll have an easier time figuring out what's going on. Thanks for
keeping me posted! I'll keep my fingers crossed.

On Friday, July 3, 2015, Andrew Havens notifications@github.com wrote:

@subvertallchris https://github.com/subvertallchris This might have
worked. I waited until I started seeing the errors again, updated my
Gemfile to your branch, bundled, then restarted the app and the errors went
away. However, restarting the app might have fixed this as well. I'll keep
running your branch over the next few days and see if the error comes back.


Reply to this email directly or view it on GitHub
#16 (comment)
.

Member

subvertallchris commented Jul 3, 2015

If it's the same root cause, the restart will always clear up the
temporarily. Hopefully, this does it permantnetly, but even if it doesn't,
I think we'll have an easier time figuring out what's going on. Thanks for
keeping me posted! I'll keep my fingers crossed.

On Friday, July 3, 2015, Andrew Havens notifications@github.com wrote:

@subvertallchris https://github.com/subvertallchris This might have
worked. I waited until I started seeing the errors again, updated my
Gemfile to your branch, bundled, then restarted the app and the errors went
away. However, restarting the app might have fixed this as well. I'll keep
running your branch over the next few days and see if the error comes back.


Reply to this email directly or view it on GitHub
#16 (comment)
.

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 3, 2015

Member

I merged the PR into Neo4j and will delete the branch soon, so you can use neo4j's master or lock at ref 05abc109c73740e3ed10216aecd54405b5047383.

Member

subvertallchris commented Jul 3, 2015

I merged the PR into Neo4j and will delete the branch soon, so you can use neo4j's master or lock at ref 05abc109c73740e3ed10216aecd54405b5047383.

@djvs

This comment has been minimized.

Show comment
Hide comment
@djvs

djvs Jul 8, 2015

Same issue, crops up after some time, restart clears up. Think it is the same as some other issues discussed with class "resolution" not quite working, right?

djvs commented Jul 8, 2015

Same issue, crops up after some time, restart clears up. Think it is the same as some other issues discussed with class "resolution" not quite working, right?

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 8, 2015

Member

Does updating to the current branch of neo4j resolve it?

On Tuesday, July 7, 2015, djvs notifications@github.com wrote:

Same issue, crops up after some time, restart clears up.


Reply to this email directly or view it on GitHub
#16 (comment)
.

Member

subvertallchris commented Jul 8, 2015

Does updating to the current branch of neo4j resolve it?

On Tuesday, July 7, 2015, djvs notifications@github.com wrote:

Same issue, crops up after some time, restart clears up.


Reply to this email directly or view it on GitHub
#16 (comment)
.

@djvs

This comment has been minimized.

Show comment
Hide comment
@djvs

djvs Jul 8, 2015

You mean the master branch?

djvs commented Jul 8, 2015

You mean the master branch?

@djvs

This comment has been minimized.

Show comment
Hide comment
@djvs

djvs Jul 8, 2015

Seems so yes

djvs commented Jul 8, 2015

Seems so yes

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 8, 2015

Member

Seems to fix it? And yeah, master branch will do it but it's really the
commit in the PR linked in this issue. It was merged in the other night.

On Tuesday, July 7, 2015, djvs notifications@github.com wrote:

Seems so yes


Reply to this email directly or view it on GitHub
#16 (comment)
.

Member

subvertallchris commented Jul 8, 2015

Seems to fix it? And yeah, master branch will do it but it's really the
commit in the PR linked in this issue. It was merged in the other night.

On Tuesday, July 7, 2015, djvs notifications@github.com wrote:

Seems so yes


Reply to this email directly or view it on GitHub
#16 (comment)
.

thermistor added a commit to thermistor/neo4j that referenced this issue Jul 21, 2015

Addresses class caching problem where classes were not being reloaded…
… properly when rails reloaded changed code.

It was leading to this issue in devise-neo4j:
neo4jrb/devise-neo4j#16
@thermistor

This comment has been minimized.

Show comment
Hide comment
@thermistor

thermistor Jul 21, 2015

Did not fix it for me. I am running neo4j 5.0.5 and devise 3.5.1 and I still got this error.

I am using a to_prepare clause in neo4j/railtie.rb and that appears to have solved the issue for me:

thermistor/neo4j@a2a31f1

Happy to make a pull request if someone could weigh in on whether this is the right approach.

thermistor commented Jul 21, 2015

Did not fix it for me. I am running neo4j 5.0.5 and devise 3.5.1 and I still got this error.

I am using a to_prepare clause in neo4j/railtie.rb and that appears to have solved the issue for me:

thermistor/neo4j@a2a31f1

Happy to make a pull request if someone could weigh in on whether this is the right approach.

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 21, 2015

Member

This will be run before every request in development, I'm not sure that's
ideal, but I'm also not a Rails expert so maybe this is how things are
done. It sounds like there's another reload event occurring that isn't
triggering the callback I added to the gem?

On Tuesday, July 21, 2015, Weston Triemstra notifications@github.com
wrote:

Did not fix it for me. I am running neo4j 5.0.5 and devise 3.5.1 and I
still got this error.

I am using a to_prepare clause in neo4j/railtie.rb and that appears to
have solved the issue for me:

thermistor/neo4j@a2a31f1
thermistor/neo4j@a2a31f1

Happy to make a pull request if someone could weigh in on whether this is
the right approach.


Reply to this email directly or view it on GitHub
#16 (comment)
.

Member

subvertallchris commented Jul 21, 2015

This will be run before every request in development, I'm not sure that's
ideal, but I'm also not a Rails expert so maybe this is how things are
done. It sounds like there's another reload event occurring that isn't
triggering the callback I added to the gem?

On Tuesday, July 21, 2015, Weston Triemstra notifications@github.com
wrote:

Did not fix it for me. I am running neo4j 5.0.5 and devise 3.5.1 and I
still got this error.

I am using a to_prepare clause in neo4j/railtie.rb and that appears to
have solved the issue for me:

thermistor/neo4j@a2a31f1
thermistor/neo4j@a2a31f1

Happy to make a pull request if someone could weigh in on whether this is
the right approach.


Reply to this email directly or view it on GitHub
#16 (comment)
.

@thermistor

This comment has been minimized.

Show comment
Hide comment
@thermistor

thermistor Jul 21, 2015

I'll see if I can create a little project that duplicates the issue later this week. I was able to cause the problem pretty easily by simple changing code in my devise model, User.rb, and then doing any action that would call Devise::Mapping.find_scope!

thermistor commented Jul 21, 2015

I'll see if I can create a little project that duplicates the issue later this week. I was able to cause the problem pretty easily by simple changing code in my devise model, User.rb, and then doing any action that would call Devise::Mapping.find_scope!

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Jul 24, 2015

Member

@thermistor 5.0.5 doesn't actually include the fix for this, you'll need to stick with master or the PR I referenced for now.

Member

subvertallchris commented Jul 24, 2015

@thermistor 5.0.5 doesn't actually include the fix for this, you'll need to stick with master or the PR I referenced for now.

@thermistor

This comment has been minimized.

Show comment
Hide comment
@thermistor

thermistor Jul 24, 2015

@subvertallchris Ah ok thanks, bad assumption on my part.

thermistor commented Jul 24, 2015

@subvertallchris Ah ok thanks, bad assumption on my part.

@subvertallchris

This comment has been minimized.

Show comment
Hide comment
@subvertallchris

subvertallchris Aug 17, 2015

Member

This fix was worked into neo4j 5.0.15, so I'm going to close the issue. Thanks for everyone's help and patience with this!

Member

subvertallchris commented Aug 17, 2015

This fix was worked into neo4j 5.0.15, so I'm going to close the issue. Thanks for everyone's help and patience with this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment