# 2024-06-22
Table of Contents
2024-06-22
second_to_last
https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-second_to_last
# そもそも順序を保証しないんだっけ。updated_at の昇順に見える> Article.ids Article Ids (18.8ms) SELECT "articles"."id" FROM "articles"=> [3, 7, 9, 5, 6, 8, 10]# どうやらそのようだ> Article.order(updated_at: :asc).ids Article Ids (0.6ms) SELECT "articles"."id" FROM "articles" ORDER BY "articles"."updated_at" ASC=> [3, 7, 9, 5, 6, 8, 10]
# これは id の降順に並べてオフセット1 で1件だけ取り出している# つまり id の昇順に並べて最後から2番目を取り出す> Article.second_to_last.id Article Load (0.5ms) SELECT "articles".* FROM "articles" ORDER BY "articles"."id" DESC LIMIT $1 OFFSET $2 [["LIMIT", 1], ["OFFSET", 1]]=> 9
up_only
https://api.rubyonrails.org/classes/ActiveRecord/Migration.html#method-i-up_only
これ知らなかった。バックフィルとか不可逆なものに対して今まで def down; end
と書いてたけど up_only
でできそう
class AddPublishedToPosts < ActiveRecord::Migration[7.1] def change add_column :posts, :published, :boolean, default: false up_only do execute "update posts set published = 'true'" end endend
Rails 5.2 で入ったんだなあ
https://koic.hatenablog.com/entry/rails-reversible-migration-cop-supports-ar-migration-up-only