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:

  1. initialization. on step messaggio assign 'my_messagio'.
  2. after_initialize callback executing. on step messaggio assign messagenotdefined 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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -