diff options
author | CassioMarques <cassiommc@gmail.com> | 2009-01-10 17:49:12 -0200 |
---|---|---|
committer | CassioMarques <cassiommc@gmail.com> | 2009-01-10 17:49:12 -0200 |
commit | cf4b24407ada79c133fbae1ec7db692882225956 (patch) | |
tree | 6abe2971cf58147d11a96ef503fdec4701a520c1 /railties/doc/guides/source | |
parent | 32c3ce1d2bf50b42481b4cc8d20d90b495bd200d (diff) | |
download | rails-cf4b24407ada79c133fbae1ec7db692882225956.tar.gz rails-cf4b24407ada79c133fbae1ec7db692882225956.tar.bz2 rails-cf4b24407ada79c133fbae1ec7db692882225956.zip |
Getting rid of the example of callback registration by overrinding the callback method name
Diffstat (limited to 'railties/doc/guides/source')
-rw-r--r-- | railties/doc/guides/source/activerecord_validations_callbacks.txt | 29 |
1 files changed, 1 insertions, 28 deletions
diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index 09cc1fdb20..29bff0c7b3 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -620,29 +620,7 @@ Callbacks are methods that get called at certain moments of an object's lifecycl === Callbacks registration -In order to use the available callbacks, you need to registrate them. There are two ways of doing that. - -=== Registering callbacks by overriding the callback methods - -You can specify the callback method directly, by overriding it. Let's see how it works using the +before_validation+ callback, which will surprisingly run right before any validation is done. - -[source, ruby] ------------------------------------------------------------------- -class User < ActiveRecord::Base - validates_presence_of :login, :email - - protected - def before_validation - if self.login.nil? - self.login = email unless email.blank? - end - end -end ------------------------------------------------------------------- - -=== Registering callbacks by using macro-style class methods - -The other way you can register a callback method is by implementing it as an ordinary method, and then using a macro-style class method to register it as a callback. The last example could be written like that: +In order to use the available callbacks, you need to registrate them. You can do that by implementing them as an ordinary methods, and then using a macro-style class method to register then as callbacks. [source, ruby] ------------------------------------------------------------------ @@ -671,11 +649,6 @@ class User < ActiveRecord::Base end ------------------------------------------------------------------ -In Rails, the preferred way of registering callbacks is by using macro-style class methods. The main advantages of using macro-style class methods are: - -* You can add more than one method for each type of callback. Those methods will be queued for execution at the same order they were registered. -* Readability, since your callback declarations will live at the beggining of your models' files. - CAUTION: Remember to always declare the callback methods as being protected or private. These methods should never be public, otherwise it will be possible to call them from code outside the model, violating object encapsulation and exposing implementation details. == Conditional callbacks |