diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-12-12 03:18:24 -0800 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-12-12 03:18:24 -0800 |
commit | 5c5d5080b7ea008f0b4a5bf2e65bb22d24094cea (patch) | |
tree | 81db8a7d138a45ee915305dba4b96b062eb51715 /guides | |
parent | 5ef040d0c6cbac6b5caf1703e899164a4c2a3a56 (diff) | |
parent | bdbb5e3a4210017ab6b984adf11bbae710db7319 (diff) | |
download | rails-5c5d5080b7ea008f0b4a5bf2e65bb22d24094cea.tar.gz rails-5c5d5080b7ea008f0b4a5bf2e65bb22d24094cea.tar.bz2 rails-5c5d5080b7ea008f0b4a5bf2e65bb22d24094cea.zip |
Merge pull request #13032 from kuldeepaggarwal/after_touch_documentation
[ci skip] added after_touch callback documentation [ci skip]
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_callbacks.md | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md index ece18a0a53..667433285f 100644 --- a/guides/source/active_record_callbacks.md +++ b/guides/source/active_record_callbacks.md @@ -141,6 +141,55 @@ You have initialized an object! => #<User id: 1> ``` +### `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') +=> #<User id: 1, name: "Kuldeep", created_at: "2013-11-25 12:17:49", updated_at: "2013-11-25 12:17:49"> + +>> 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 +=> #<Employee id: 1, company_id: 1, created_at: "2013-11-25 17:04:22", updated_at: "2013-11-25 17:05:05"> + +# triggers @employee.company.touch +>> @employee.touch +Employee/Company was touched +An Employee was touched +=> true +``` + Running Callbacks ----------------- |