diff --git a/app/models/decklist.rb b/app/models/decklist.rb index fbc93a9..55a5647 100644 --- a/app/models/decklist.rb +++ b/app/models/decklist.rb @@ -4,7 +4,7 @@ # # Decklists are the public, published decklists owned by various users. class Decklist < ApplicationRecord - # TODO(plural): Add a relationship to a public user object. + belongs_to :user belongs_to :side has_one :identity_card, # rubocop:disable Rails/InverseOf diff --git a/app/models/review.rb b/app/models/review.rb index 903df0b..7258616 100644 --- a/app/models/review.rb +++ b/app/models/review.rb @@ -3,6 +3,7 @@ # Model for reviews of cards. class Review < ApplicationRecord belongs_to :card + belongs_to :user, optional: true has_many :review_comments has_many :review_votes diff --git a/app/models/review_comment.rb b/app/models/review_comment.rb index 0909a19..3271c2c 100644 --- a/app/models/review_comment.rb +++ b/app/models/review_comment.rb @@ -3,4 +3,5 @@ # Model for comments on reviews. class ReviewComment < ApplicationRecord belongs_to :review + belongs_to :user, optional: true end diff --git a/app/models/review_vote.rb b/app/models/review_vote.rb index 35741df..ed446cd 100644 --- a/app/models/review_vote.rb +++ b/app/models/review_vote.rb @@ -3,4 +3,5 @@ # Model for votes on reviews. class ReviewVote < ApplicationRecord belongs_to :review + belongs_to :user, optional: true end diff --git a/app/models/user.rb b/app/models/user.rb index 77c464b..ad28d81 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,4 +5,8 @@ # This object will remain fairly lean since user management will not be handled in the application itself. class User < ApplicationRecord has_many :decks + has_many :decklists + has_many :reviews + has_many :review_comments + has_many :review_votes end diff --git a/db/migrate/20260520044700_add_missing_foreign_keys.rb b/db/migrate/20260520044700_add_missing_foreign_keys.rb new file mode 100644 index 0000000..2ac37e3 --- /dev/null +++ b/db/migrate/20260520044700_add_missing_foreign_keys.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +class AddMissingForeignKeys < ActiveRecord::Migration[8.1] + def change + # User constraints + add_foreign_key :decklists, :users + add_foreign_key :reviews, :users + add_foreign_key :review_comments, :users + add_foreign_key :review_votes, :users + + # card_faces constraints + add_foreign_key :card_faces, :cards + + # card_faces_card_subtypes constraints + add_foreign_key :card_faces_card_subtypes, :card_subtypes + + # Composite foreign key for card_faces_card_subtypes -> card_faces + reversible do |dir| + dir.up do + execute <<~SQL + ALTER TABLE card_faces_card_subtypes + ADD CONSTRAINT fk_card_faces_card_subtypes_card_faces + FOREIGN KEY (card_id, face_index) + REFERENCES card_faces (card_id, face_index); + SQL + end + dir.down do + execute <<~SQL + ALTER TABLE card_faces_card_subtypes + DROP CONSTRAINT fk_card_faces_card_subtypes_card_faces; + SQL + end + end + + # illustrators_printings constraints + add_foreign_key :illustrators_printings, :illustrators + add_foreign_key :illustrators_printings, :printings + + # printing_faces constraints + add_foreign_key :printing_faces, :printings + + # printings_card_subtypes constraints + add_foreign_key :printings_card_subtypes, :printings + add_foreign_key :printings_card_subtypes, :card_subtypes + end +end diff --git a/db/schema.rb b/db/schema.rb index 8869ee7..613ffc6 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[8.1].define(version: 2026_05_09_183917) do +ActiveRecord::Schema[8.1].define(version: 2026_05_20_044700) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "pgcrypto" @@ -383,6 +383,9 @@ t.datetime "updated_at", null: false end + add_foreign_key "card_faces", "cards" + add_foreign_key "card_faces_card_subtypes", "card_faces", column: ["card_id", "face_index"], primary_key: ["card_id", "face_index"], name: "fk_card_faces_card_subtypes_card_faces" + add_foreign_key "card_faces_card_subtypes", "card_subtypes" add_foreign_key "card_pools", "formats" add_foreign_key "card_pools_card_cycles", "card_cycles" add_foreign_key "card_pools_card_cycles", "card_pools" @@ -400,6 +403,7 @@ add_foreign_key "cards_card_subtypes", "cards" add_foreign_key "decklists", "cards", column: "identity_card_id" add_foreign_key "decklists", "sides" + add_foreign_key "decklists", "users" add_foreign_key "decklists_cards", "cards" add_foreign_key "decklists_cards", "decklists" add_foreign_key "decks", "cards", column: "identity_card_id" @@ -408,8 +412,13 @@ add_foreign_key "decks_cards", "cards" add_foreign_key "decks_cards", "decks" add_foreign_key "factions", "sides" + add_foreign_key "illustrators_printings", "illustrators" + add_foreign_key "illustrators_printings", "printings" + add_foreign_key "printing_faces", "printings" add_foreign_key "printings", "card_sets" add_foreign_key "printings", "cards" + add_foreign_key "printings_card_subtypes", "card_subtypes" + add_foreign_key "printings_card_subtypes", "printings" add_foreign_key "restrictions", "formats" add_foreign_key "restrictions_card_subtypes_banned", "card_subtypes" add_foreign_key "restrictions_card_subtypes_banned", "restrictions" @@ -424,8 +433,11 @@ add_foreign_key "restrictions_cards_universal_faction_cost", "cards" add_foreign_key "restrictions_cards_universal_faction_cost", "restrictions" add_foreign_key "review_comments", "reviews" + add_foreign_key "review_comments", "users" add_foreign_key "review_votes", "reviews" + add_foreign_key "review_votes", "users" add_foreign_key "reviews", "cards" + add_foreign_key "reviews", "users" add_foreign_key "rulings", "cards" add_foreign_key "snapshots", "card_pools" add_foreign_key "snapshots", "formats"