created user model

This commit is contained in:
osabic2004@gmail.com 2024-08-07 16:59:14 +02:00
parent f3b67b0800
commit ffc09cabc4
5 changed files with 23 additions and 1 deletions

View File

@ -1,6 +1,7 @@
class Article < ApplicationRecord class Article < ApplicationRecord
include Visible include Visible
belongs_to: :user
has_many :comments, dependent: :destroy has_many :comments, dependent: :destroy
validates :title, presence: true validates :title, presence: true

View File

@ -1,3 +1,5 @@
class User < ApplicationRecord class User < ApplicationRecord
has_many :articles, dependent: :destroy
has_secure_password has_secure_password
end end

View File

@ -1,6 +1,9 @@
class CreateUsers < ActiveRecord::Migration[7.1] class CreateUsers < ActiveRecord::Migration[7.1]
def change def change
create_table :users do |t| create_table :users do |t|
t.string :username
t.string :email
t.string :password_digest
t.timestamps t.timestamps
end end

View File

@ -0,0 +1,5 @@
class AddUserReferenceToArticles < ActiveRecord::Migration[7.1]
def change
add_reference :articles, :user, foreign_key: true
end
end

13
db/schema.rb generated
View File

@ -10,13 +10,15 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "articles", force: :cascade do |t|
t.string "title" t.string "title"
t.text "body" t.text "body"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.string "status" t.string "status"
t.integer "user_id"
t.index ["user_id"], name: "index_articles_on_user_id"
end end
create_table "comments", force: :cascade do |t| 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" t.index ["article_id"], name: "index_comments_on_article_id"
end 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" add_foreign_key "comments", "articles"
end end