From bdbb5e3a4210017ab6b984adf11bbae710db7319 Mon Sep 17 00:00:00 2001 From: Kuldeep Aggarwal Date: Mon, 25 Nov 2013 18:14:13 +0530 Subject: [ci skip] added after_touch callback documentation [ci skip] added more explanation for `after_touch` callback --- guides/source/active_record_callbacks.md | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'guides') diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md index ac5e8ffc0c..6cd7848f68 100644 --- a/guides/source/active_record_callbacks.md +++ b/guides/source/active_record_callbacks.md @@ -141,6 +141,55 @@ You have initialized an object! => # ``` +### `after_touch` + +The `after_touch` callback will be called whenever an Active Record object is touched. + +```ruby +class User < ActiveRecord::Base + after_touch do |user| + puts "You have touched an object" + end +end + +>> u = User.create(name: 'Kuldeep') +=> # + +>> u.touch +You have touched an object +=> true +``` + +It can be used along with `belongs_to`: + +```ruby +class Employee < ActiveRecord::Base + belongs_to :company, touch: true + after_touch do + puts 'An Employee was touched' + end +end + +class Company < ActiveRecord::Base + has_many :employees + after_touch :log_when_employees_or_company_touched + + private + def log_when_employees_or_company_touched + puts 'Employee/Company was touched' + end +end + +>> @employee = Employee.last +=> # + +# triggers @employee.company.touch +>> @employee.touch +Employee/Company was touched +An Employee was touched +=> true +``` + Running Callbacks ----------------- -- cgit v1.2.3