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

时间:2014-04-13 15:50:56

标签: ruby-on-rails ruby 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

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

1 个答案:

答案 0 :(得分:15)

回调方法必须是protectedprivate

Types of callbacks

部分下的http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html开始