简体   繁体   中英

Add trackable (devise) to existing User model?

Issue: I previously created a User model with minimal devise information. I saw devise has a "trackable" system and wanted to implement it into the existing model. I added:

class AddSignInCountToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :sign_in_count, :integer
  end
end

To test out the migrations and it won't work. (i also tried this migration with "default: 0, null: false" with same results.

The issues seem to happen once I add ":trackable" into the devise model.

I receive this error when i sign in:

NoMethodError in Devise::SessionsController#create
undefined method `current_sign_in_at' for #<User:0x00007f484e5ef770>
#line with red highlight
        match ? attribute_missing(match, *args, &block) : super

Error in CMD first few lines :

NoMethodError (undefined method `current_sign_in_at' for #<User:0x00007f484e5ef770>):

activemodel (5.2.1) lib/active_model/attribute_methods.rb:430:in `method_missing'

Routes:

  devise_for :users, controllers: { confirmations: 'confirmations' }

Model:

  devise :database_authenticatable, :registerable,
          :confirmable, :recoverable, :rememberable, :validatable, :trackable

        def active_for_authentication?
          super && approved
        end

        def inactive_message
          approved? ? super : :not_approved
        end
...
...
...

Is this a devise issue or is this being caused elsewhere?

Is the issue also possibly because of the existing model (not sure why this would be true but just in case), because if so, I could override t and recreate it since this is still in development.

I want to take advantage of all Devise has to offer and want to migrate the rest of the devise features into my model. Anyone got a suggestion or two?

The Trackable module expects more than just the sign_in_count attribute. The full list of required columns is listed in the documentation If you add another migration for the remaining columns, everything should work as expected.

ANSWER:

(found this shortly after on SO: NoMethodError in Devise::SessionsController#create undefined method `current_sign_in_at' )

The answer awarded the green check helped.

I first tried simply adding trackable, didn't work and wouldn't migrate for some reason (didn't save the error)

But their first recc to use for example:

rake db:migrate:down VERSION=20140126101944 # use version of the user migration
rake db:migrate up VERSION=20140126101944 # use version of the user migration

The only issue i ran into from this was all the other migrations i added to the users table didn't migrate along with it (name, etc.) so i had to re migrate evertyhing back over.

To clarify, as someone else suggested, i needed more columns than just the one for trackable to work. So that was probably the overall issue here.

For trackable to work as expected, you'll need all of its required columns.

Step by step, it goes like this.

First, create a new migration file with:

rails generate migration AddDeviseTrackableColumnsToUsers

Edit your newly create file to include the following:

class AddDeviseTrackableColumnsToUsers < ActiveRecord::Migration[6.1]
  def change
    add_column :users, :sign_in_count, :integer, default: 0, null: false
    add_column :users, :current_sign_in_at, :datetime
    add_column :users, :last_sign_in_at, :datetime
    add_column :users, :current_sign_in_ip, :string
    add_column :users, :last_sign_in_ip, :string
  end
end

Run your migration:

rails db:migrate

Finally, update your User model to activate trackable:

class User < ApplicationRecord
  devise :database_authenticatable, :trackable
end

And it should work now 🤗

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM