![How requirements shaped my code, AKA Rails 5 and ActiveRecord before_destroy callbacks](https://cdn-ak-scissors.b.st-hatena.com/image/square/8d71ff5111e05619a10d29bb40d7aebaa75c8fbc/height=288;version=1;width=512/https%3A%2F%2Fs0.wp.com%2Fi%2Fblank.jpg)
class Foo < ActiveRecord::Base has_many :bars, dependent: :destroy before_destroy :do_something def do_something return if bars.blank? bars.map &:somethig_cool end end このように, has_many(has_one)な関係をものをbefore_destroyで使うには注意が必要. 上の例だとbefore_destroyの前にbarsは削除されてしまうので, bars.blank?は必ず真になりbars.map &:somethig_coolは永久に実行されない. これを避けるには以下のようにする. class Foo < ActiveRecord::Base before_destroy :do_something has_
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く