ruby - Best Way to initialize a instance variable in Rails -
i'm having trouble initializing instance variables in rails, need use variable in different methods, needs initialized beforehand, eg:
class test < activerecord::base @test = 1 def testing @test+1 end end t = test.new t.testint
i follow error:
test.rb:4:in `testar': undefined method `+' nil:nilclass (nomethoderror) test.rb:9:in `<main>'
is there more elegant way initialize variable without using after_initialize
?:
if dont want use after_initialize
, create variable on fly:
attr_writer :test def testing self.test += 1 end def test @test ||= 0 end
Comments
Post a Comment