为什么会出现此错误?
NoMethodError: undefined method `set_password' for Service (call 'Service.connection' to establish a connection):Class
这是我的模型(命名为Service)
class Service < ApplicationRecord
def set_password
self.password = p SecureRandom.base64(5)
self.save
end
end
感谢您的回答。
您必须在类类(例如Service.set_password)而不是实例(例如Service.new.set_password或Service.first.set_password)上调用方法。
阅读类和实例方法之间的区别:
http://ruby-for-beginners.rubymonstas.org/writing_classes/methods.html