constructor in model

Hi all,

I have an activation model that when constructed should set the key
(column in the activations table) to test.

class Activation < ActiveRecord::Base
  belongs_to :user

  def initialize
    this.key = "test"
  end
end

When i do ac = Activation.new I gget
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
from C:/rails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/attribute_methods.rb:202:in `method_missing'
from C:/Documents and Settings/AIR14607/My Documents/prive/rails/
TeamGeist/app/models/activation.rb:5:in `initialize'
        from (irb):2:in `new'
        from (irb):2

Someone knows why?

thanks

Hi all,

I have an activation model that when constructed should set the key
(column in the activations table) to test.

class Activation < ActiveRecord::Base
belongs_to :user

def initialize
   this.key = "test"
end
end

When i do ac = Activation.new I gget
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
from C:/rails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/attribute_methods.rb:202:in `method_missing'
from C:/Documents and Settings/AIR14607/My Documents/prive/rails/
TeamGeist/app/models/activation.rb:5:in `initialize'
       from (irb):2:in `new'
       from (irb):2

Someone knows why?

You haven't called the superclass initialize

Fred

Is this something for which a before_create callback would work?

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

I always use this syntax:

  def initialize(params = nil)
    super(nil)
    self.whatever ||= "blarg"
  end