J'ai remarqué que Rails ne déclenche pas de rappel lorsque le symbole de rappel est passé en entrée.ActiveRecord: Ne pas appeler after_initialize lorsque le nom de la méthode est passé en tant que symbole
Le code ci-dessous ne fonctionne pas.
class User < ActiveRecord::Base
after_initialize :init_data
def init_data
puts "In init_data"
end
end
Le code ci-dessous fonctionne.
class User < ActiveRecord::Base
def after_initialize
init_data
end
def init_data
puts "In init_data"
end
end
Quelqu'un peut-il expliquer ce comportement?
Note 1
ActiveRecord documentation dit le texte suivant à propos after_initialize
:
Unlike all the other callbacks, after_find and after_initialize will
only be run if an explicit implementation is defined (def after_find).
In that case, all of the callback types will be called.
Bien qu'il soit indiqué que after_initialize exige la mise en œuvre explicite, je trouve la deuxième phrase dans le paragraphe ci-dessus ambiguë, à savoir In that case, all of the callback types will be called.
Qu'est-ce que all of the call back types
?
L'exemple de code dans la documentation a un exemple qui n'utilise pas la mise en œuvre explicite:
after_initialize EncryptionWrapper.new
J'ai essayé de faire ma méthode de rappel privé, et j'ai toujours eu le même comportement. –