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 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 db/migrate/20250512181245_add_latest_post_at_to_stream.rb (limited to 'db/migrate/20250512181245_add_latest_post_at_to_stream.rb') 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 -- cgit 1.4.1