Gaeguri
Gaeguri

Reputation: 509

No such column users.remember_token

I try to use rememberable on my app in really simple way, but when I check the remember_me checkbox I got the following error

SQLite3::SQLException: no such column: users.remember_token: SELECT "users".* FROM "users" WHERE "users"."remember_token" = ? ORDER BY "users"."id" ASC LIMIT ?

Here is my User migration

class MigrateUserToDevise < ActiveRecord::Migration[5.2]
  def change
    change_table :users, bulk: true do |t|
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
       t.integer  :sign_in_count, default: 0, null: false
       t.datetime :current_sign_in_at
       t.datetime :last_sign_in_at
       t.string   :current_sign_in_ip
       t.string   :last_sign_in_ip

       add_index :users, :nickname,             unique: true
       add_index :users, :reset_password_token, unique: true
    end
  end
end

and here is the form

<div class="login">
  <h3>로그인</h3>

  <div>
    <% flash.each do |name, msg| -%>
        <%= content_tag :div, msg, class: name %>
    <% end -%>

    <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
      <div class="field">
        <%= f.label "ID" %><br />
        <%= f.text_field :nickname, autofocus: true, class: "input_classic" %>
      </div>

      <div class="field">
        <%= f.label "비밀번호" %><br />
        <%= f.password_field :password, autocomplete: "current-password", class: "input_classic" %>
      </div>

      <% if devise_mapping.rememberable? %>
        <div class="field">
          <%= f.check_box :remember_me %>
          <%= f.label :remember_me %>
        </div>
      <% end %>

      <div class="actions">
        <%= f.submit "Log in" %>
      </div>
    <% end %>

    <%= render "devise/shared/links" %>
  </div>
</div>

I don't think I should need a remember_token column in my user model but I don't understand why I got this error.

Thank you

EDIT:

Here is my User.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable

  attr_accessor :remember_token, :reset_token

  has_many :favorites, dependent: :delete_all
  has_many :favorite_articles, through: :favorites, source: :article, dependent: :delete_all
  has_many :upvotes, dependent: :nullify
  has_many :upvote_articles, through: :upvotes, source: :article, dependent: :nullify
  has_many :reports, dependent: :nullify
  has_many :report_articles, through: :reports, source: :article
  has_many :report_comments, through: :reports, source: :comment
  has_many :comments, dependent: :nullify
  has_many :articles, dependent: :nullify

  validates :nickname, uniqueness: true, presence: true, exclusion: { in: %w(admin developer modo),
    message: "%{value} is reserved." }
  validates :password, presence: true, length: { minimum: 5 }, allow_nil: true

  serialize :saved_article, Array

  # Override update_without_password
  def update_without_password(params, *options)
    if params[:password].blank?
      params.delete(:password)
      params.delete(:password_confirmation) if params[:password_confirmation].blank?
    end

    result = update_attributes(params, *options)
    clean_up_passwords
    result
  end
end

Upvotes: 0

Views: 166

Answers (1)

Alexis
Alexis

Reputation: 5075

Since you added a remember_token attr_accessor, you are overwriting the devise module method, here is the declaration https://github.com/heartcombo/devise/blob/master/lib/devise/models/rememberable.rb#L147

Changing the name of the attribute should fix the problem.

Upvotes: 1

Related Questions