I'm trying to initialize and instance variable as an array as follows:
class Arch < ActiveRecord::Base
attr_accessor :name1
def initialize
@name1 = []
end
def add_name1(t)
@name1 << t
end
end
When I try Arch.new in a console session I get (Object doesn't support #inspect). What's up? How do I make an instance array variable? I tried to follow this like so:
class Arch < ActiveRecord::Base
attr_accessor :name1
def after_initialize
@name1 = []
end
def add_name1(t)
@name1 << t
end
end
and my @name1 was still a NilClass. :/
You are overriding ActiveRecord's
initialize
method. Try usingsuper
:You are overiding the
initialize
method ofActiveRecord::Base
. When creating a new instance of your class only your initilize will be called. All the instance variables thatActiveRecord::Base
would have created are uninitialized and#inspect
fails. In order to fix this you need to call the constructor of your base class (usingsuper
)