Open1

ActiveRecord autosave

朝型のジェシー朝型のジェシー

https://github.com/rails/rails/issues/50173

サンプルコードは後で追加したい気持ち

class User < ActiveRecord::Base
  has_many :articles, autosave: true
end

class Article < ActiveRecord::Base
  belongs_to :user
  validates :name, uniqueness: true
end

uniqueness 判定を貫通する件

  • 既存レコード(persisted)との重複は検出されるが、そうでない(build しているだけ)ものの場合は見過ごされる
    • ※ not null 制約のような、他のバリデーションは通る
    • name が同じ article を2件 build していたら?
      • 保存できてしまう
    • 保存済みのレコードと重複していたら?
      • 弾かれる
    • mark_for_destruction していて、この後save!で削除されるものと重複していたら?
      • 弾かれる
        • 未保存だからpersisted な状態としては存在しているため