Tôi nhận thấy Rails không kích hoạt gọi lại after_initialize
khi biểu tượng gọi lại được chuyển làm đầu vào.ActiveRecord: Không gọi after_initialize khi tên phương thức được truyền dưới dạng một biểu tượng
Mã bên dưới không hoạt động.
class User < ActiveRecord::Base
after_initialize :init_data
def init_data
puts "In init_data"
end
end
Mã bên dưới hoạt động.
class User < ActiveRecord::Base
def after_initialize
init_data
end
def init_data
puts "In init_data"
end
end
Ai đó có thể giải thích hành vi này?
Note 1
Các ActiveRecord documentation nói sau đây về 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.
Mặc dù nó được tuyên bố rằng after_initialize đòi hỏi thực hiện rõ ràng, tôi tìm ra câu thứ hai trong đoạn trên mơ hồ, tức là In that case, all of the callback types will be called.
all of the call back types
là gì?
Các mẫu mã trong tài liệu có một ví dụ mà không sử dụng thực hiện rõ ràng:
after_initialize EncryptionWrapper.new
tôi đã cố gắng làm cho phương pháp gọi lại tôi tin, và tôi vẫn có những hành vi tương tự. –