Ruby-on-rails-rails-callback-functions
提供:Dev Guides
Ruby on Rails-コールバック関数
アクティブなレコードオブジェクトのライフサイクル中に、8つのイベントにフックできます-
- (-) セーブ
- (-)有効ですか?
- before_validation
- before_validation_on_create
- (-) 検証
- (-)validate_on_create
- after_validation
- after_validation_on_create
- before_save
- before_create
- (-)作成
- after_create
- after_save
例
class Subscription < ActiveRecord::Base
before_create :record_signup
private
def record_signup
self.signed_up_on = Date.today
end
end
class Firm < ActiveRecord::Base
# Destroys the associated clients and
# people when the firm is destroyed
before_destroy{
|record|Person.destroy_all "firm_id= #{record.id}"
}
before_destroy{
|record|Client.destroy_all "client_of= #{record.id}"
}
end
コールバック関数の詳細については、http://guides.rubyonrails.org/active_record_callbacksl [コールバック関数]リンクを確認してください。