views:

270

answers:

2

ActiveRecord has a few different callback methods used to simplify model logic. For example after_find and before_create methods.

Consider this code example:

class ExternalPrintingCard < ActiveRecord::Base
  belongs_to :user
  belongs_to :ph_user

  after_create :change_pin

  def change_pin
    self.user.randomize_printer_pin
  end

  def after_find
    return if self.card_status == false
    self.card_status = false if self.is_used_up?
    self.card_status = false if self.is_expired?
    self.save!
  end
end

If I remove all the self prefixes from the instance variables or instance methods, these 2 callbacks will be called, but it is as if they are local variables inside these callback methods.

This instance variable (card_status), instance methods (save!, is_used_up? and is_expired?) and association (user) worked fine outside these 2 callback methods without the self prefix.

The sample code in the Rails' documentation for callback methods (instance methods), seems to always use the self prefix even though it is calling instance variables or methods, which by right they are accessible without the self prefix normally.

I hope someone with a better understanding of ActiveRecord callbacks can help to shed a light on this behaviour.

Cheers

+5  A: 

Technically you only need to use the self in front of the assignment methods. This is necessary to differentiate between a instance method with trailing = and an assignment to a local variable.

nasmorn
See this Thoughtbot article for many details: http://robots.thoughtbot.com/post/185504560/to-self-or-not-to-self
François Beausoleil
another article that explains it http://www.rubyfleebie.com/use-self-explicitly/
Peer Allan
A: 

Nasmorn is correct.

ActiveRecord::Base placed all the column names inside @attributes instance variable (Hash) and create accessors instance methods for those column names.

For example:

card_status is a column in external_printing_cards table, it will have accessor methods with the name card_status and card_status=

Since ruby local variable definition is dynamic, the line

def after_find
  ....  
  card_status = false if self.is_used_up?
  ....
end

will mean we are defining and assigning to a local variable card_status rather than the instance method card_status=

The article that Peer Allan posted provides more explanation on this.

SamChandra