From b7c5853de5f0f04625eab6389cba9de8b02e48fb Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 12 May 2025 14:53:31 -0400 Subject: Added streams index --- .../20250512181245_add_latest_post_at_to_stream.rb | 21 +++++++++++++++++++++ db/schema.rb | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20250512181245_add_latest_post_at_to_stream.rb (limited to 'db') diff --git a/db/migrate/20250512181245_add_latest_post_at_to_stream.rb b/db/migrate/20250512181245_add_latest_post_at_to_stream.rb new file mode 100644 index 0000000..9d753e3 --- /dev/null +++ b/db/migrate/20250512181245_add_latest_post_at_to_stream.rb @@ -0,0 +1,21 @@ +class AddLatestPostAtToStream < ActiveRecord::Migration[7.1] + def up + add_column :streams, :latest_post_at, :datetime + + Stream.all.each do |stream| + if stream.updates.empty? + stream.latest_post_at = stream.created_at + else + stream.latest_post_at = stream.updates.order(created_at: :desc).first.created_at + end + + stream.save! + end + + change_column_null :streams, :latest_post_at, false + end + + def down + remove_column :streams, :latest_post_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 795a986..76e9ad8 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: 2025_01_12_025207) do +ActiveRecord::Schema[7.1].define(version: 2025_05_12_181245) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -358,6 +358,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_01_12_025207) do t.string "slug" t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false + t.datetime "latest_post_at", null: false end create_table "taggings", force: :cascade do |t| -- cgit 1.4.1