我正在尝试使用表Users的记录来播种我的生产数据库。这是模型:
class User < ActiveRecord::Base
include Person
include Contact
has_many :coworkers, :class_name => 'Coworker'
has_many :customers, :class_name => 'Customer'
has_many :locations, :class_name => 'Location'
has_many :appointment_types, :class_name => 'AppointmentType'
before_save { self.email = email.downcase }
#before_create :create_remember_token
# password
has_secure_password
attr_accessor :remember_token, :activation_token, :reset_token
validates :password, length: { minimum: 6 }, on: :create
# rem_notice_hrs
validates :rem_notice_hrs, presence: true
validates :rem_notice_hrs, numericality: true
# rem_text
validates :rem_text, presence: true
# mandatory email:
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
after_initialize :init
#def create_remember_token
# self.remember_token = Account.digest(Account.new_remember_token)
#end
# Returns the hash digest of the given string.
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# Returns a random token.
def User.new_token
SecureRandom.urlsafe_base64
end
# Remembers a user in the database for use in persistent sessions.
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
# Returns true if the given token matches the digest.
def authenticated?(attribute, token)
digest = send("#{attribute}_digest")
return false if digest.nil?
BCrypt::Password.new(digest).is_password?(token)
end
# Forgets a user.
def forget
update_attribute(:remember_digest, nil)
end
# Sets the password reset attributes.
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
# Returns true if a password reset has expired.
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
private
def init
if self.new_record?
if self.rem_notice_hrs.nil?
self.rem_notice_hrs = 24
end
if self.rem_text.nil?
if self.company.nil?
self.rem_text = "Dear [customer title: automatic] [customer family name: automatic], this is a reminder of your appointment with %{title} %{family_name} on [date/time]."
else
self.rem_text = "Dear [title] [customer family name], this is a reminder of your appointment with %{company} on [date/time]."
end
end
if self.start_day.nil?
self.start_day = Time.now
end
end
end
end
这是种子:
User.create!(first_name: "Nick",
email: "user@example.com",
password: "user12",
password_confirmation: "user12",
phone: 7933336337,
rem_notice_hrs: 24,
rem_text: "This is a reminder",
start_day: "2014-11-21 21:50:16",
)
我正在通过capistrano运行种子任务,我得到了:
ActiveRecord::RecordInvalid: Validation failed: Password is too short (minimum is 6 characters) /home/deploy/appmate/shared/bundle/ruby/2.1.0/gems/activerecord-4.2.0/lib/active_record/validations.rb:79:in `raise_record_invalid' /home/deploy/appmate/shared/bundle/ruby/2.1.0/gems/activerecord-4.2.0/lib/active_record/validations.rb:43:in `save!' /home/deploy/appmate/shared/bundle/ruby/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:29:in `save!' /home/deploy/appmate/shared/bundle/ruby/2.1.0/gems/activerecord-4.2.0/lib/active_record/transactions.rb:291:in `block in save!'
这里出了什么问题?密码正好有6个字符。不应出现验证错误?!
答案 0 :(得分:0)
我在开发中遇到了同样的问题,但我还没有找到解决方法。现在我要关闭密码验证。
validates_confirmation_of :password
validates_length_of :password, :within => 4..20
validates_presence_of :password, :if => :password_required?
从localhost:3000 / users / new我输入newuser@emailaddress.com / password我收到错误:
2个错误禁止此用户被保存:
密码太短(最少4个字符)密码不能 空白