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

时间:2022-12-22 01:20:53

When I try u = User.new followed by u.save, the before_save method is not being fired.

当我尝试u = User.new后跟u.save时,不会触发before_save方法。

Here's my User model:

这是我的用户模型:

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/

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

1 个解决方案

#1


14  

The callback methods need to be either protected or private.

回调方法需要是受保护的或私有的。

From here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html under the section Types of callbacks

从这里:http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html下回调类型

#1


14  

The callback methods need to be either protected or private.

回调方法需要是受保护的或私有的。

From here: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html under the section Types of callbacks

从这里:http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html下回调类型