RoR:attr_encrypted没有在数据库上保存数据

时间:2013-04-06 11:30:29

标签: ruby-on-rails ruby-on-rails-3.2 attr-encrypted

我在rails 3.2.13中使用attr_encrypted来加密列。为此,在我的模型中,我有以下内容:

attr_encrypted :social_security_no, :key => 'a secret key'

该应用不会在数据库中保存social_security_noencrypted_social_security_no

我也尝试了spectator-attr_encrypted gem。但是,现在它发出以下错误:

/home/ashish/.rvm/gems/ruby-2.0.0-p0@lendty/gems/activerecord-3.2.13/lib/active_record/dynamic_matchers.rb:55:in 'method_missing': undefined method 'attr_encrypted' for #<Class:0x0000000824f768> (NoMethodError)

那么,有什么方法可以摆脱这个问题吗?或者,是否有一个分叉版本的gem可以正常使用Rails 3.2.13和Ruby 2.0.0?

而且,这是我的模特:

class Lender < ActiveRecord::Base
  extend SignUpCounter
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :first_name, :last_name, :evening_phone, :daytime_phone, :social_security_no
  attr_encrypted :social_security_no, :key => '3243serw54325325435sdrtf34453454325sdt346546'

  # Validations
  validates_uniqueness_of :social_security_no, :email

  # Geocoding 
  geocoded_by :current_sign_in_ip
  after_validation :geocode

  # Associations
  has_many :loans
  has_many :borrowers, :through => :loans

  # Scopes
  scope :verified, where(verified: true)

  def full_name
    first_name.to_s + " " + last_name
  end

end

1 个答案:

答案 0 :(得分:1)

只是一个猜测,但我认为问题出在validates_unqiueness_of中。我的一位同事通过编写自己的验证来解决这个问题。

validate :must_have_a_valid_email


def must_have_a_valid_email
  if email.present?
    leaders = Leader.where(:encrypted_email => Leader.encrypt_email(self.email))
    leaders = leaders.where('id != ', self.id) if self.persisted?
    self.errors.add(:email, "This email address is in use") if leaders.count > 0
  end
end
相关问题