Code:
#schema..
t.integer "shares", :default => 0
end
#model
def increment_shares
shares = shares + 1 #undefined method + for nil class
save
end
#console
irb(main):003:0> t.shares
=> 0
irb(main):004:0> t.shares = t.shares + 1
=> 1
irb(main):005:0> t.increment_shares
NoMethodError: undefined method `+' for nil:NilClass
(Problem should be clear from the code. Writing more text to meet ‘quality standards’)
You should use
selfto access a model’s attributes :