📄 16 - adding hooks to table events.rb
字号:
require 'cookbook_dbconnect'require 'og'require 'glue/aspects'class BlogPost property :title, :content, String after :on => :og_insert do |post| puts %{Sending email notification of new post "#{post.title}"} # Actually send the email here... endendog_connectpost = BlogPost.newpost.title = 'Robots are taking over'post.content = 'Think about it! When was the last time you saw another human?'post.save!# Sending email notification of new post "Robots are taking over"#---require 'cookbook_dbconnect'activerecord_connectclass BlogPost < ActiveRecord::Base def after_create puts %{Sending email notification of new blog post "#{title}"} # Actually send the email here... endendpost = BlogPost.create(:title => 'Robots: Gentle Yet Misunderstood', :content => 'Popular misconceptions about robERROR 40')# Sending email notification of new blog post "Robots: Gentle Yet Misunderstood#---require 'cookbook_dbconnect'activerecord_connectclass BlogPost < ActiveRecord::Baseendclass MailObserver < ActiveRecord::Observer observe BlogPost def after_create(post) puts %{Sending email notification of new blog post "#{post.title}"} # Actually send the email here. endendActiveRecord::Base.observers = MailObserverpost = BlogPost.new(:title => "ERROR 40", :content => "ERROR ERROR ERROR ERROR ERROR")post.save# Sending email notification of new blog post "ERROR 40"#---# environment.rbconfig.active_record.observers = MailObserver#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -