class User < ActiveRecord::Base
attr_accessor :password
Rails.logger.info "xxy From outside"
def before_create
Rails.logger.info "xxy From inside the before_create"
end
end
在控制器中调用
User.save
时,我的开发日志选择了xxy From outside
而不是xxy From inside the before_create
,所以我以为它已被弃用是对的吗?如果是这样,如何在保存之前调用模型方法?还是被记录为
xxy From outside
,这是否意味着在保存模型实例时会自动调用所有方法? 最佳答案
They are still there.您似乎做错了。这是正确的方法:
# Define callback:
before_create :method_name
# and then:
def method_name
Rails.logger.info "I am rad"
end