简体   繁体   English

将可跟踪(设计)添加到现有用户模型?

[英]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. (我还尝试使用“默认值:0,null:false”进行此迁移,结果相同。

The issues seem to happen once I add ":trackable" into the devise model.一旦我将“:trackable”添加到设计模型中,这些问题似乎就会发生。

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 : CMD 前几行错误

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.问题是否也可能是因为现有模型(不知道为什么这是真的,但以防万一),因为如果是这样,我可以覆盖 t 并重新创建它,因为它仍在开发中。

I want to take advantage of all Devise has to offer and want to migrate the rest of the devise features into my model.我想利用 Devise 提供的所有功能,并希望将其余的设计功能迁移到我的模型中。 Anyone got a suggestion or two?任何人有一个或两个建议?

The Trackable module expects more than just the sign_in_count attribute. Trackable模块不仅需要sign_in_count属性。 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' ) (不久后在 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:但他们的第一个 recc 使用例如:

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.我遇到的唯一问题是我添加到用户表的所有其他迁移都没有随之迁移(名称等),所以我不得不重新迁移evertyhing。

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.要使 trackable 按预期工作,您将需要其所有必需的列。

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 🤗它现在应该可以工作了🤗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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