为什么`before_save`在Rails 4中不起作用?

Sha*_*ane 8 ruby ruby-on-rails ruby-on-rails-4

当我尝试u = User.new后跟时u.save,该before_save方法没有被触发.

这是我的User模特:

class User < ActiveRecord::Base
  attr_accessor :password
  before_save :encrypt_password

  def encrypt_password
    puts "Its getting called"
  end
end
Run Code Online (Sandbox Code Playgroud)

https://railsforum.com/topic/1741-rails-4-and-before-save-method/

小智 16

回调方法必须是protectedprivate.

从这里:http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html下的部分Types of callbacks

  • puts "Hello" 仍然没有向 rails 控制台打印任何内容。在控制台中打印的任何特定方法。 (2认同)