diff --git a/app/models/article.rb b/app/models/article.rb index 0aa494e..57ec8c1 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,6 +1,7 @@ class Article < ApplicationRecord include Visible + belongs_to: :user has_many :comments, dependent: :destroy validates :title, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index d67da20..9ecd5e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,5 @@ class User < ApplicationRecord + has_many :articles, dependent: :destroy + has_secure_password end diff --git a/db/migrate/20240807144034_create_users.rb b/db/migrate/20240807144034_create_users.rb index 1ec6d0d..158eba6 100644 --- a/db/migrate/20240807144034_create_users.rb +++ b/db/migrate/20240807144034_create_users.rb @@ -1,6 +1,9 @@ class CreateUsers < ActiveRecord::Migration[7.1] def change create_table :users do |t| + t.string :username + t.string :email + t.string :password_digest t.timestamps end diff --git a/db/migrate/20240807145348_add_user_reference_to_articles.rb b/db/migrate/20240807145348_add_user_reference_to_articles.rb new file mode 100644 index 0000000..3d66921 --- /dev/null +++ b/db/migrate/20240807145348_add_user_reference_to_articles.rb @@ -0,0 +1,5 @@ +class AddUserReferenceToArticles < ActiveRecord::Migration[7.1] + def change + add_reference :articles, :user, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 44b7e69..b04d1fa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,13 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_08_07_123956) do +ActiveRecord::Schema[7.1].define(version: 2024_08_07_145348) do create_table "articles", force: :cascade do |t| t.string "title" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "status" + t.integer "user_id" + t.index ["user_id"], name: "index_articles_on_user_id" end create_table "comments", force: :cascade do |t| @@ -29,5 +31,14 @@ ActiveRecord::Schema[7.1].define(version: 2024_08_07_123956) do t.index ["article_id"], name: "index_comments_on_article_id" end + create_table "users", force: :cascade do |t| + t.string "username" + t.string "email" + t.string "password_digest" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_foreign_key "articles", "users" add_foreign_key "comments", "articles" end