diff --git a/app/models/conversation.rb b/app/models/conversation.rb new file mode 100644 index 0000000..a80d3cf --- /dev/null +++ b/app/models/conversation.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Conversation < ApplicationRecord + belongs_to :user + + has_many :messages, dependent: :destro +end diff --git a/app/models/message.rb b/app/models/message.rb new file mode 100644 index 0000000..18d711a --- /dev/null +++ b/app/models/message.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class Message < ApplicationRecord + belongs_to :conversation +end diff --git a/app/models/user.rb b/app/models/user.rb index 8804aac..9dad2cf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,4 +4,6 @@ class User < ApplicationRecord has_secure_password validates :email, presence: true, uniqueness: true + + has_many :conversations, dependent: :destroy end diff --git a/db/migrate/20250301202016_create_conversations.rb b/db/migrate/20250301202016_create_conversations.rb new file mode 100644 index 0000000..9640fdf --- /dev/null +++ b/db/migrate/20250301202016_create_conversations.rb @@ -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 diff --git a/db/migrate/20250301202033_create_messages.rb b/db/migrate/20250301202033_create_messages.rb new file mode 100644 index 0000000..e0d0919 --- /dev/null +++ b/db/migrate/20250301202033_create_messages.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 548a637..8162576 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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