4

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

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

该应用程序既不保存social_security_no也不保存encrypted_social_security_no在数据库中。

我也试过spectator-attr_encrypted宝石。但是,现在它给出了以下错误:

/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)

那么,有什么办法可以摆脱这个问题呢?或者,是否有适用于 Rails 3.2.13 和 Ruby 2.0.0 的 gem 的分叉版本?

而且,这是我的模型:

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
4

1 回答 1

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
于 2013-04-19T13:35:48.620 回答