http://railsapi.masuidrive.jp/module/ActiveRecord::Associations::ClassMethods/has_many

 :dependent - if set to :destroy all the associated objects are destroyed alongside
 this object by calling their destroy method.

つまり、

 class foo < ActiveRecord::Base
   has_many :bar, :dependent => :destory
 end

のようにしておき、fooのオブジェクトをdestoryすると、barのdestoryも呼び出されるそうです。

If set to :delete_all all associated objects are deleted without calling their
destroy method.
 class foo < ActiveRecord::Base
   has_many :bar, :dependent => :delete_all
 end

だと、fooをdestoryすると、barのdestoryを呼び出さずにdeleteされるそうです。

If set to :nullify all associated objects’ foreign keys are set to NULL without
calling their save callbacks.
 class foo < ActiveRecord::Base
   has_many :bar, :dependent => :nullify
 end

だと、fooをdestoryすると、barのsaveを呼び出すことなく、barのfooへのforeign keyを NULL
にセットするらしいです。

NULL可なりレーションの時の対処ですね。

NOTE: :dependent => true is deprecated and has been replaced with :dependent => :destroy.

:dependent => trueは :dependent => :destoryと同じ。


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

アークウェブのサービスやソリューションはこちら