Callbacks before_save does not work for me ruby on rails

0

hello the error is that I'm not saving the id as a string in the variable
   fullnumid I just want to be saved when they are created, thanks

after_create :guardar
 def guardar
  self.fullnumid = self.id.to_s
end
    
asked by juan gomez 13.10.2017 в 04:52
source

1 answer

0

You need to save the object after assigning the value to fullnumid ; you can do it with save! or with update! :

With save! :

def guardar
  self.fullnumid = id.to_s
  save!
end

With update! :

def guardar
  update!(fullnumid: id.to_s)
end

You may notice the use of save! and update! (with ! ), which is useful so that, in case of failure, an error is thrown (if you remove the ! it also works, but you can not see the error in case the record does not update).

    
answered by 14.10.2017 в 22:37