-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
undefined method `will_save_change_to_email?' for User #4542
Comments
I have the same problem. I'm using mongoid 6.1 and Active Record. According to the comment in 3e1c9e3 it's not working at the moment. Also related to this issue. |
I am not sure what exactly transpired to have caused this error. It seems that all of a sudden I'm getting the same So, a quick monkey patch, at least for the time being, is to just define
This resolves the problem, at least in my case it did. The issue here is that method
is not even defined in the devise gem. Although it is referenced in the callback, in the following files:
it is not defined whatsoever. Please note that this problem is only raised and replicated when using Mongoid as of today's date, 7/9/2017. The problem does not exist when using Active Record. |
I've facing this issue with ActiveRecord.
|
Do you have the mongoid gem installed? You probably do, as one could still have both, Active Record and Mongoid in the same app. I don't think the issue is stemming from Active Record, if that was the case, then a significant amount of Rails apps would break, and it would have been resolved a long time ago, not exactly sure what the source of the issue is other than what I described above. At any rate, did you define the method
And it worked like a charm. I'm using:
Naturally, when using Mongoid for example, it seems that Devise is calling a "will_save_change_to_email?" method without defining it, it's probably depending on mongoid supplying that method, I think. So, whatever the reason, I simply defined the missing method, and it ended up working great for me. If you ended up resolving the problem another way, please share your solution with the community on here, perhaps there's a better way than monkey-patching stuff :) |
So it turns out that the model does not have an email attribute, and both The project is one I've inherited and had the pleasure of taking from Rails 3 all the way up to 5.1, while switching from MySQL to Postgresql. It's been, interesting... TL;DR:
works fine for me! |
Hello @mtoribio, thanks for your report. Thank you! |
I got a same error. My environment is below.
I dont use mongoid, Im using mysql. And the table we use for devise doesnt have column the name of 'email'. Sorry I dont have time to create public repo, but I wish this will help you. |
@nao215 what devise modules are you using in your model? validatable requires an email attribute and we also call this method on confirmable (but I guess you don't use confirmable since you don't have an email column) |
this is our code for devise. The error happened when we try to up rails version to 5. We used validatable from rails 4 and we version uped. Thanks for you reply and sorry for dirty english. |
@nao215 But even when you hit the |
Yes, but the error is |
@nao215 What I meant is that is weird this worked before you update rails, because even though |
I've run into this as well. The problem appears to be that devise only makes a global check for |
@mbell697 Can you provide us a sample application that reproduces the issue in isolation? |
@tegon If you modify your test suite to load both Mongoid and ActiveRecord, but test Mongoid backed device models, you'll likely see a bunch of failures. Can probably just make a quick change here (https://github.com/plataformatec/devise/blob/master/test/rails_app/config/application.rb#L12) to always load active record and run the suite with |
I ran into this as well, from a clean new rails application. STR: 1.) rails new auth_test You can fix this problem by not including ActiveRecord: |
Related sample application |
If you are using Rails 5.x with MongoID and NOT active record then you must comment out "require 'active_storage/engine'" in the application.rb. Active storage uses Active record. Example require_relative 'boot'
require 'rails'
# Pick the frameworks you want:
require 'active_model/railtie'
require 'active_job/railtie'
# require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
require 'action_cable/engine'
require 'sprockets/railtie'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module ReadSharedBooks
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
# Don't generate system test files.
config.generators.system_tests = nil
end
end |
@mtoribio @aalbagarcia @codewizardry @chrise86 @tegon please have a look on this |
I am still face problem with this.. I am using
I need both mongoid and activerecord on my system.. the User class uses mongoid and has a field called email. Everything was working perfectly on rails 5.0 but since I updated Rails to 5.1.7 I get this message Any updates on this? |
@chrisokamoto Unfortunately, I haven't been able to dig deep into this yet. I'll try to do it soon. |
setting this 3 aliases "solved" the problem for me for now:
|
I define Default prefixes and suffixes are defined in Edit: I actually might need to do something more than that because the generic methods declared in |
create a function in model file
|
For info... In Rails 5.2.4.3:
In Rails 5.2.4.5
This appears to be causing Devise.activerecord51? to be From the Mongoid docs...
|
After removing Under Rails 5.2.4.3, defined?(ActiveRecord) returns UPDATE: DatabaseCleaner gem was including ActiveRecord. Use |
DEVICE GEM IS WORKING FINE NO ISSUE AT ALL Here is my following setup Rails 5.2.6 In file "config/initializers/devise.rb" require 'devise/orm/mongoid' ProblemDevice thinks you are using ActiveRecord even though you have created your rails application with the option WhyYou have a gem that is not compatible with mongoid and is using ActiveRecord monkey patching. HowWhen device try to detect if you are using ActiveRecord be using the code below defined?(ActiveRecord) && ActiveRecord.gem_version >= Gem::Version.new("5.1.x") it comes up false positive. SolutionEnter rails console and type in NameError: uninitialized constant ActiveRecord
from (pry):13:in `<main>' If you don't then start removing gems until you see that error message. For me it was the following gems that gave me grief:
I replaced "cromo" with "whenever", "database_cleaner" with "database_cleaner-mongoid" |
@SaimonL what you explained here makes sense to me. Is anyone still having this kind of issue? Please let me know so I can try to take another look, otherwise I'm gonna close it in a week or two. |
Yes - the behavior is still the same. What @SaimonL described is a possible trigger, but in our case it's much simpler - our application uses both Mongoid and ActiveRecord. At the base level, the problem is that Devise currently checks for the presence of the Line 311 in 8e2e3f6
This drives a lot of choices down stream, e.g.
The problem is that the presence of the To clarify - Mongoid implements the AR <5.1 API in this case, so the AR5.1 check is acting as both "Is this AR or Mongoid?" and "what version of AR is this?" at the same time which is a little confusing. |
@mbell697 thanks for the information, I think that makes sense. I believe Devise never considered being used in an app that has both ORMs loaded -- even though I think it'd be possible to even use Devise in both at the same time by requiring both ORMs at the initializer.
Sounds like this could work yeah, I will see if I can look into some changes there. I want to get Devise running tests with latest versions of Mongo as well at some point. |
Devise is able to work with a specific ORM, either Active Record or Mongoid, but nothing stops apps from using multiple ORMs within the same application -- they just need to pick one to use with Devise. That's generally determined by the require that is added to the Devise initializer, that will load up either ORM's extensions so you can call things like `devise` on your model to set it up. However, some conditional logic in Devise, more specifically around dirty tracking, was only considering having Active Record loaded up after a certain version, to determine which methods to call in parts of the implementation. In a previous change we refactored all that dirty tracking code into this `OrmDirtyTracking` module to make it easier to view all the methods that were being conditionally called, and now we're repurposing this into a more generic `Orm` module (that's nodoc'ed by default) so that upon including it, we can conditionally include the proper dirty tracking extensions but also check whether the including model is really Active Record or not, so we can trigger the correct dirty tracking behavior for Mongoid as well if both are loaded on the same app, whereas previously the Mongoid behavior would always use the new Active Record behavior, but support may differ. While we are also working to ensure the latest versions of Mongoid are fully running with Devise, this should improve the situation by giving apps with multiple ORMs loaded a chance to rely on some of these Devise bits of functionality better now that weren't working properly before without some monkey-patching on their end. Closes #5539 Closes #4542
Okay, I have a possible fix for this scenario with multiple ORMs loaded in #5576, and I'm also working to reenable Mongoid tests in #5568 (it's all green up to Mongoid 7.x, but Mongoid 8 will need some additional work as it apparently introduced changes to dirty tracking as well) That should be enough to fix these issues I hope. |
Hi,
I have problems with:
In rails 5.0 thats works good.
I have been investigating a little more and I not found where is defined will_save_change_to_email?.
I think thats could be the problem...
For now, I will use Rails 5.0 but I would like use Rails 5.1.
Thanks in advance.
The text was updated successfully, but these errors were encountered: