Rails callback executed after all fields have been initialized -
how can create callback in model gets called after fields have been initialized?
i tried using after_initialize
callback so:
class article < activerecord::base after_initialize :print_self def print_self pp self end
however, @ time fields nil
, ilustrated print statement:
#<article:0x007f8bea51a298 id: nil, name: nil, body: nil, url: nil, published_at: nil, created_at: nil, updated_at: nil, guid: nil, summary: nil>
the callback have created in above code correct. issue see here don't see code initializing model such 'new', assign values attributes.
in absence of this, none of attributes have values.
you may ask, why 'id' not have value, because ids created , assigned when record created/saved.
you need along lines of:
article.new(:name => 'my article')
in controller or go rails console , following:
article = article.new(:name => 'my article')
to initialize article.
Comments
Post a Comment