views:

49

answers:

1

I have the following code in an Active Record file.

class Profile < ActiveRecord::Base
  attr_accessor :tmp_postal_code

  def postal_code=(postal_code)
    @temp_postal_code = postal_code[:first] + "-" + postal_code[:second]
  end

  def postal_code
    @temp_postal_code
  end
end

I first overwrote postal_code=(postal_code) because params in a controller is a hash e.g., ({:first => "1234", :second => "9999"}). Second, when I tried use a getter method, I got nil, so I added the getter method. In order to share the value of the postal_code attribute, I made @temp_postal_code.

Now everything works except for one. Look at the console output below.

>> p = Profile.new
  SQL (0.1ms)   SET NAMES 'utf8'
  SQL (0.1ms)   SET SQL_AUTO_IS_NULL=0
  Profile Columns (1.3ms)   SHOW FIELDS FROM `profiles`
=> #<Profile id: nil, name: nil, pr: "", age: nil, postal_code: nil>
>> p.postal_code = {:first => "123", :second => "9999"}
=> {:second=>"9999", :first=>"123"}
>> p.postal_code
=> "123-9999"
>> p.postal_code
=> "123-9999"
>> p.name = "TK"
=> "TK"
>> p.postal_code
=> "123-9999"
>> p.pr = "Hello"
=> "Hello"
>> p.age = 20
=> 20
>> p
=> #<Profile id: nil, name: "TK", pr: "Hello", age: 20, postal_code: nil> # WHY!!!
>> p.postal_code
=> "123-9999"

When I try to access postal_code attribute individually by p.postal_code, the value exists. But when I try to show p, postal_code is nil. It looks like the latter is used for save operation. I cannot save the postal code with a meaningful value.

Something is wrong with my understanding with virtual attributes and overwriting of attributes. By further exploration, I noticed the difference between hash notation and dot notation.

  >> p[:postal_code]
  => nil
  >> p[:name]
  => "TK"
  >> p.postal_code
  => "123-9999"

I have no idea why this occurs. I want to be able to save with a postal_code filled in.

A: 

You want to use the super method so that it actually gets put into the AR attributes.

  def postal_code=(postal_code)
    super(postal_code[:first] + "-" + postal_code[:second])
  end

  # you shouldn't even need this anymore
  def postal_code
    @temp_postal_code
  end

You wont need the attr_accessor anymore either. Hope that helps.

John Duff
Thanks a lot. Now everything works file.
TK