0

In a rails 4 (currently 4.0.10) application model I was having a problem with a method not defined for NilClass error in a view, and so I decided to try to solve by setting a sometime nil to an empty string in the model's after_initialize thusly:

def after_initialize
  ...
  self.biography ||= ""
end

because, according to the docs here http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html , "after_initialize callback is triggered for each object that is found and instantiated by a finder" yet, it appears that after_initialize never gets called and so the error persists, as tested in the rails console:

2.1.1 :001 > Presenter.find(2)
 => #<Presenter id: 2, mediahub_id: 2, name: "Craig", asset_id: nil, biography: nil, status: "active", created_at: "2015-08-12 23:00:34", 
updated_at: "2015-08-12 23:00:34"> 

as you can see, biography is still nil after the find. What am I doing wrong? :)

Craig
  • 419
  • 4
  • 19
  • As you figured out, the reason is stated [here](http://stackoverflow.com/questions/328525/how-can-i-set-default-values-in-activerecord). Thanks for finding out the link. – Arup Rakshit Aug 13 '15 at 19:40
  • @ArupRakshit - I'd like you to get credit for your answer as it was essentially right but did you remove it? Can you put it back so I can accept it? – Craig Aug 13 '15 at 20:13
  • its ok mate! :) No need to accept. You had a problem in understanding, and you got it, that's all. – Arup Rakshit Aug 14 '15 at 06:34

0 Answers0