From cf4b24407ada79c133fbae1ec7db692882225956 Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Sat, 10 Jan 2009 17:49:12 -0200 Subject: Getting rid of the example of callback registration by overrinding the callback method name --- .../html/activerecord_validations_callbacks.html | 40 ++-------------------- 1 file changed, 2 insertions(+), 38 deletions(-) (limited to 'railties/doc/guides/html') diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index bdd5e723ea..0862776f53 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -285,10 +285,6 @@ ul#navMain {
  • Callbacks registration
  • -
  • Registering callbacks by overriding the callback methods
  • - -
  • Registering callbacks by using macro-style class methods
  • -
  • @@ -649,7 +645,7 @@ http://www.gnu.org/software/src-highlite --> Note -If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true +If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in ⇒ [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # ⇒ true

    The default error message for validates_presence_of is "can’t be empty".

    @@ -1069,26 +1065,7 @@ An object of the ActionView::Helpers::InstanceTag class.

    Callbacks are methods that get called at certain moments of an object’s lifecycle. With callbacks it’s possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.

    9.1. Callbacks registration

    -

    In order to use the available callbacks, you need to registrate them. There are two ways of doing that.

    -

    9.2. 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.

    -
    -
    -
    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
    -

    9.3. 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.

    before_create {|user| user.name = user.login.capitalize if user.name.blank?} 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. -

      -
    • -
    -- cgit v1.2.3