Skip to content

Commit 12e35fc

Browse files
committed
Ensure legacy collection items have order set
1 parent a0c722a commit 12e35fc

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class SetOrderOnCollectionItems < ActiveRecord::Migration[7.0]
2+
class Collection < ApplicationRecord
3+
has_many :items, -> { order(:order) }, class_name: 'CollectionItem'
4+
end
5+
6+
class CollectionItem < ApplicationRecord
7+
belongs_to :collection
8+
end
9+
10+
def up
11+
Collection.find_each do |collection|
12+
indexes = Hash.new(0)
13+
collection.items.sort_by(&:order).each do |item|
14+
item.update_column(:order, indexes[item.resource_type] += 1)
15+
end
16+
end
17+
end
18+
19+
def down
20+
end
21+
end

db/schema.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2023_08_03_095429) do
14-
13+
ActiveRecord::Schema[7.0].define(version: 2023_10_13_132536) do
1514
# These are extensions that must be enabled in order to support this database
1615
enable_extension "plpgsql"
1716

0 commit comments

Comments
 (0)