Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Activerecord callbacks

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 273
    Comment on it

    Callbacks are methods that get invoked at various stages of an Activerecord objects life cycle. and thus it is possible to write code that will run whenever an Activerecord object is created, saved, updated, deleted, validated, or loaded from the database.

    Below is the list of the callbacks that are available in rails framework:

    Creating an Object

    • before_validation ,after_validation, before_save ,around_save, before_create ,around_create, after_create, after_save

    Updating an Object

    • before_validation ,after_validation, before_save ,around_save, before_update ,around_update, after_update ,after_save

    Destroying an Object

    • before_destroy, around_destroy, after_destroy

    Some examples of callback use:

    • before validation:

      class User < ActiveRecord::Base validates :name, :first_name ,:last_name , :presence => true

      before_validation :ensure_user_has_a_name

      protected def ensure_user_has_a_name self.name = first_name + + last_name unless first_name.nil && last_name.nil

      end

      end

    • before_save:

      class Location < ActiveRecord::Base

      validates :street_name, :city, :zip, :state, :country , :presence => true

      before_save :ensure_address

      protected

      def ensure_address self.address = [street_name , city , zip , state , country].compact.join(,)

      end

      end

    before_save is a callback that is invoked both while saving new record or updating existing record.

    There are some methods which do not trigger callbacks . They are :

    • decrement, decrement_counter, delete, delete_all, find_by_sql, increment, increment_counter, toggle, touch, update_column, update_all, update_counters

    Conditional Callbacks:

    We can make the execution of a callback method conditional on the satisfaction of a given condition. We can do this using the :if and :unless options, which can take a symbol, a string or a Proc. You may use the :if option when you want to specify under which conditions the callback should be called. If you want to specify the conditions under which the callback should not be called, then you may use the :unless option.For example:

    class Device < ActiveRecord::Base
    
      before_save :check_gateway
    
      def check_gateway
       self.gateway = 'gateway.sandbox.push.apple.com' if file_path.present? && gateway.blank?
      end
    end
    

    Using proc as condition for callback.

     class Order < ActiveRecord::Base
        before_save :normalize_card_number,:if => Proc.new { |order| order.paid_with_card? }
     end
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: