ruby on rails - saving an object value passed by function -
i have folliwing code:
class logfactory < activerecord::base after_initialize :inizializza messagenotdefined = "msg" def inizializza self.happened = time.current self.messaggio = messagenotdefined end def setmessage(messaggio) logger = logfactory.new(:messaggio => messaggio, :happened => self.happened) logger.save end end
the problem in messaggio
variable. mean, if use param messaggio in .new(:messaggio => messaggio,..
rails still use messagenotdefined
constant defined during initialization. why?
cause assign messagenotdefined
messaggio
after object initialization. when .new(:messaggio => 'my_messaggio', ...)
, there 2 steps:
- initialization. on step
messaggio
assign'my_messagio'
. after_initialize
callback executing. on stepmessaggio
assignmessagenotdefined
anyway, according code.
it looks want use this:
def inizializza happened ||= time.current messaggio ||= messagenotdefined end
this means messagenotdefined
assign messaggio
if falsey or has not initialized yet. also, both self
redundant.
Comments
Post a Comment