I'm having trouble initializing instance variables in Rails, need to use a variable in different methods, but this needs to be initialized beforehand, eg:
class Test < ActiveRecord::Base
@test = 1
def testing
@test+1
end
end
t = Test.new
t.testing
I get the follow error:
test.rb:4:in `testar': undefined method `+' for nil:NilClass (NoMethodError)
from test.rb:9:in `<main>'
Is there a more elegant way to initialize a variable without using the after_initialize?:
initialize, invoked onnew)?after_initializeis the way to go@test = 1hasselfpointing to your class, not to your instance.