Ruby on Rails / Active Record Notes Basic 5 – Callbacks & Observers

Six common Active Record callbacks, 1 less:

before_create

after_create

before_save

after_save

before_destroy

after_destroy

after_initialize

example, add this to your model, and it will never be created…:

def before_create

false

end

Observers

$ rails generate observer Comment

class CommentObserver < ActiveRecord::Observer

def after_create(comment)

puts “We will notify the author”

end

end

config/application.rb:

config.active_record.observers = :comment_observer

Leave a Reply

You must be logged in to post a comment.