From 6b43cfa8130b82b49b15d0c0bf61cf16fdfdd979 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Sat, 6 Jan 2024 11:10:56 +0000 Subject: [PATCH] Add tag association to learning goals --- app/controllers/learning_goals_controller.rb | 1 + app/models/learning_goal.rb | 3 +++ app/models/tag.rb | 1 + app/views/learning_goals/_form.html.erb | 3 +++ db/migrate/20240106103226_learning_goal_tag_association.rb | 5 +++++ db/schema.rb | 7 ++++++- 6 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20240106103226_learning_goal_tag_association.rb diff --git a/app/controllers/learning_goals_controller.rb b/app/controllers/learning_goals_controller.rb index 5d8d981..2407579 100644 --- a/app/controllers/learning_goals_controller.rb +++ b/app/controllers/learning_goals_controller.rb @@ -56,6 +56,7 @@ class LearningGoalsController < ApplicationController :retrospective, :completed, :user, + tag_ids: [], ) end diff --git a/app/models/learning_goal.rb b/app/models/learning_goal.rb index 879fdf3..f818af9 100644 --- a/app/models/learning_goal.rb +++ b/app/models/learning_goal.rb @@ -5,4 +5,7 @@ class LearningGoal < ApplicationRecord has_rich_text :retrospective validates :title, presence: true + + has_and_belongs_to_many :tags + accepts_nested_attributes_for :tags end diff --git a/app/models/tag.rb b/app/models/tag.rb index 320e815..dbee19b 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -4,6 +4,7 @@ class Tag < ApplicationRecord validates :name, presence: true has_many :microposts_tags has_many :microposts, through: :microposts_tags + has_and_belongs_to_many :learning_goals def to_param name diff --git a/app/views/learning_goals/_form.html.erb b/app/views/learning_goals/_form.html.erb index 0c06f0b..110de80 100644 --- a/app/views/learning_goals/_form.html.erb +++ b/app/views/learning_goals/_form.html.erb @@ -16,6 +16,9 @@ <%= f.label :completed %> <%= f.check_box :completed %> + <%= f.label :microposts_tag %> + <%= f.collection_check_boxes :tag_ids, Tag.all, :id, :name %> + <%= f.submit button_text %> <% end %> diff --git a/db/migrate/20240106103226_learning_goal_tag_association.rb b/db/migrate/20240106103226_learning_goal_tag_association.rb new file mode 100644 index 0000000..bb8d74b --- /dev/null +++ b/db/migrate/20240106103226_learning_goal_tag_association.rb @@ -0,0 +1,5 @@ +class LearningGoalTagAssociation < ActiveRecord::Migration[7.1] + def change + create_join_table :learning_goals, :tags + end +end diff --git a/db/schema.rb b/db/schema.rb index e07295c..a2af531 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: 2024_01_06_093517) do +ActiveRecord::Schema[7.1].define(version: 2024_01_06_103226) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -108,6 +108,11 @@ ActiveRecord::Schema[7.1].define(version: 2024_01_06_093517) do t.index ["user_id"], name: "index_learning_goals_on_user_id" end + create_table "learning_goals_tags", id: false, force: :cascade do |t| + t.bigint "learning_goal_id", null: false + t.bigint "tag_id", null: false + end + create_table "microposts", force: :cascade do |t| t.bigint "user_id", null: false t.datetime "created_at", null: false