diff --git a/app/models/item.rb b/app/models/item.rb index aaf0b174..48d86834 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -10,6 +10,9 @@ class Item < ApplicationRecord SwfAssetType = 'object' + serialize :cached_compatible_body_ids, coder: Serializers::IntegerSet + serialize :cached_occupied_zone_ids, coder: Serializers::IntegerSet + has_many :closet_hangers has_one :contribution, as: :contributed, inverse_of: :contributed has_one :nc_mall_record @@ -258,8 +261,8 @@ class Item < ApplicationRecord end def update_cached_fields - self.cached_occupied_zone_ids = occupied_zone_ids.sort.join(",") - self.cached_compatible_body_ids = compatible_body_ids.sort.join(",") + self.cached_occupied_zone_ids = occupied_zone_ids + self.cached_compatible_body_ids = compatible_body_ids self.save! end diff --git a/db/migrate/20241003004726_allow_null_in_items_cached_fields.rb b/db/migrate/20241003004726_allow_null_in_items_cached_fields.rb new file mode 100644 index 00000000..d52ddc41 --- /dev/null +++ b/db/migrate/20241003004726_allow_null_in_items_cached_fields.rb @@ -0,0 +1,9 @@ +class AllowNullInItemsCachedFields < ActiveRecord::Migration[7.2] + def change + # This is a bit more compatible with ActiveRecord's `serialize` utility, + # which seems pretty insistent that empty arrays should be saved as `NULL`, + # rather than the empty string our serializer would return if called :( + change_column_null :items, :cached_compatible_body_ids, true + change_column_null :items, :cached_occupied_zone_ids, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 3f097e7a..a2d592c6 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.2].define(version: 2024_10_01_052510) do +ActiveRecord::Schema[7.2].define(version: 2024_10_03_004726) do create_table "alt_styles", charset: "utf8mb4", collation: "utf8mb4_unicode_520_ci", force: :cascade do |t| t.integer "species_id", null: false t.integer "color_id", null: false @@ -137,8 +137,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_10_01_052510) do t.text "description", size: :medium, null: false t.string "rarity", default: "", null: false t.integer "dyeworks_base_item_id" - t.string "cached_occupied_zone_ids", default: "", null: false - t.text "cached_compatible_body_ids", default: "", null: false + t.string "cached_occupied_zone_ids", default: "" + t.text "cached_compatible_body_ids", default: "" t.index ["dyeworks_base_item_id"], name: "index_items_on_dyeworks_base_item_id" t.index ["modeling_status_hint", "created_at", "id"], name: "items_modeling_status_hint_and_created_at_and_id" t.index ["modeling_status_hint", "created_at"], name: "items_modeling_status_hint_and_created_at" diff --git a/lib/serializers/integer_set.rb b/lib/serializers/integer_set.rb new file mode 100644 index 00000000..e78bdb47 --- /dev/null +++ b/lib/serializers/integer_set.rb @@ -0,0 +1,11 @@ +module Serializers + module IntegerSet + def self.dump(array) + array.sort.join(",") + end + + def self.load(string) + (string || "").split(",").map(&:to_i) + end + end +end \ No newline at end of file