From b75f9277ccc443e0a28bfa369d844cf310064ec9 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Thu, 19 Oct 2023 18:35:48 +0100 Subject: [PATCH] Add micropost/tag association --- app/models/micropost.rb | 3 +++ app/models/microposts_tag.rb | 6 ++++++ app/models/tag.rb | 2 ++ .../20231019172703_create_join_table_tags_microposts.rb | 8 ++++++++ db/schema.rb | 9 ++++++++- 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 app/models/microposts_tag.rb create mode 100644 db/migrate/20231019172703_create_join_table_tags_microposts.rb diff --git a/app/models/micropost.rb b/app/models/micropost.rb index 89b9d52..e783493 100644 --- a/app/models/micropost.rb +++ b/app/models/micropost.rb @@ -7,4 +7,7 @@ class Micropost < ApplicationRecord validates :user, :content, presence: true + + has_many :microposts_tags + has_many :tags, through: :microposts_tags end diff --git a/app/models/microposts_tag.rb b/app/models/microposts_tag.rb new file mode 100644 index 0000000..e03249a --- /dev/null +++ b/app/models/microposts_tag.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class MicropostsTag < ApplicationRecord + belongs_to :micropost + belongs_to :tag +end diff --git a/app/models/tag.rb b/app/models/tag.rb index b44e9fa..c15af97 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -2,4 +2,6 @@ class Tag < ApplicationRecord validates :name, presence: true + has_many :microposts_tags + has_many :microposts, through: :microposts_tags end diff --git a/db/migrate/20231019172703_create_join_table_tags_microposts.rb b/db/migrate/20231019172703_create_join_table_tags_microposts.rb new file mode 100644 index 0000000..cd2be83 --- /dev/null +++ b/db/migrate/20231019172703_create_join_table_tags_microposts.rb @@ -0,0 +1,8 @@ +class CreateJoinTableTagsMicroposts < ActiveRecord::Migration[7.1] + def change + create_join_table :tags, :microposts, primary_key: [:tag_id, :micropost_id] do |t| + t.index [:tag_id, :micropost_id] + t.index [:micropost_id, :tag_id] + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 7149b7b..eea9689 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2023_10_19_164620) do +ActiveRecord::Schema[7.1].define(version: 2023_10_19_172703) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -70,6 +70,13 @@ ActiveRecord::Schema[7.1].define(version: 2023_10_19_164620) do t.index ["user_id"], name: "index_microposts_on_user_id" end + create_table "microposts_tags", id: false, force: :cascade do |t| + t.bigint "tag_id", null: false + t.bigint "micropost_id", null: false + t.index ["micropost_id", "tag_id"], name: "index_microposts_tags_on_micropost_id_and_tag_id" + t.index ["tag_id", "micropost_id"], name: "index_microposts_tags_on_tag_id_and_micropost_id" + end + create_table "tags", force: :cascade do |t| t.string "name", null: false t.datetime "created_at", null: false