Scaffold models

main
Sebskyo 2025-03-01 22:26:27 +01:00
parent 5fd3cd3469
commit 11d208ef56
6 changed files with 58 additions and 1 deletions

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class Conversation < ApplicationRecord
belongs_to :user
has_many :messages, dependent: :destro
end

5
app/models/message.rb Normal file
View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class Message < ApplicationRecord
belongs_to :conversation
end

View File

@ -4,4 +4,6 @@ class User < ApplicationRecord
has_secure_password
validates :email, presence: true, uniqueness: true
has_many :conversations, dependent: :destroy
end

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
class CreateConversations < ActiveRecord::Migration[8.0]
def change
create_table :conversations do |t|
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end

View File

@ -0,0 +1,13 @@
# frozen_string_literal: true
class CreateMessages < ActiveRecord::Migration[8.0]
def change
create_table :messages do |t|
t.text :body
t.boolean :reply, null: false, default: false
t.references :conversation, null: false, foreign_key: true
t.timestamps
end
end
end

21
db/schema.rb generated
View File

@ -10,7 +10,23 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_02_24_102503) do
ActiveRecord::Schema[8.0].define(version: 2025_03_01_202033) do
create_table "conversations", force: :cascade do |t|
t.integer "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_conversations_on_user_id"
end
create_table "messages", force: :cascade do |t|
t.text "body"
t.boolean "reply", default: false, null: false
t.integer "conversation_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["conversation_id"], name: "index_messages_on_conversation_id"
end
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "password_digest", null: false
@ -18,4 +34,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_02_24_102503) do
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
end
add_foreign_key "conversations", "users"
add_foreign_key "messages", "conversations"
end