• Home
  • Blog
blog.shmn7iii.net
  • About
  • Links
  • 🔍
  • Home
  • Blog
  • About
  • Links
March 26th, 2024

saved_change_to_attribute? が便利

Ruby on Rails

前回の保存時、statusがAからBに値が変更されたかどうかを確認したい時

some_record.saved_change_to_attribute?(:status, from: A, to: B)

これで書ける。便利すぎ。

from, to なしでもいける。

some_record.saved_change_to_attribute?(:status)

参考

似たようなのまだある

active_recordのattributeメソッドまとめ - とりあえずコード書けよ
github.com ちょっと使う機会があったのでまとめ。 変更前後で値がとれるのは便利。
Favicon of the bookmark site
https://ys3128.hatenadiary.jp
Site image of the bookmark
ActiveRecord attribute methodまとめ(その②: Dirty編) - Qiita
はじめに 今回はActiveRecordのattribute methodのうち、Dirtyメソッドをまとめていきます。 (2022年6月時点。Rails7.0対応。) 以前、BeforeTypeCastの内容をまとめましたが、今回のDirtyメソッドはBeforeTyp…
Favicon of the bookmark site
https://qiita.com
Site image of the bookmark
Powered by astro-notion-blog
GitHub
X
Wantedly