Help us understand the problem. What is going on with this article?

devise導入時、既にUserモデルがある場合の対応

既にUserモデルがあるんだけど、deviseでUserモデルを作成するときはどおすればいいんだろう?

結論:普段通りdeviseを導入する形で大丈夫です🙆‍♀️

導入前

db/schema.rb
create_table "users", force: :cascade do |t|
    t.string "name"
    t.integer "age"
    t.string "sex"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

手順

既にUserモデルが存在します。

  1. gemfileにdeviseの追加
  2. bundle install
  3. deviseのインストールする rails g devise:install
  4. deviseでUserモデルのインストールする rails g devise User
  5. rails db:migrate

4.の時点でuser.rbにインサートされているのが分かります。

create    db/migrate/20191021010023_add_devise_to_users.rb
insert    app/models/user.rb
route  devise_for :users

❇︎モデルの新規作成か否かはrailsが判断してくれるそうです
❇︎既にあるUserモデルのカラムと被りが合った場合はmigrationに失敗するらしいです

導入後

db/schema.rb
create_table "users", force: :cascade do |t|
    t.string "name"
    t.integer "age"
    t.string "sex"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした