3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveRecord はそのインスタンスを create したのか update したのか

Last updated at Posted at 2018-03-17

ある record を save したときに、 create のときだけ次に何かを処理したい(しかも、コールバック以外で)ということがありました。

とりあえず思いついた方法は

> record.id_previously_changed?
=> true

Rails のコードを読むと

    def create_or_update(*args, &block)
      _raise_readonly_record_error if readonly?
      return false if destroyed?
      result = new_record? ? _create_record(&block) : _update_record(*args, &block)
      result != false
    end    

ということなので、 create / update を分離しているだけで、それぞれの中を読んでも、特に区別するためのデータはなさそう。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?