Closed2

ActiveRecordで条件付きdependent: delete_all

pentacloudpentacloud
class User < ApplicationRecord
  has_many :posts, dependent: :delete_all
end

class User < ApplicationRecord
  has_many :posts

  before_destroy :delete_relations, if -> { condition }

  private

  def delete_relations
    posts.delete_all(:delete_all)
  end
end
pentacloudpentacloud

ポイント

このスクラップは2023/06/15にクローズされました