RoR: attr_encrypted не сохраняет данные в базе данных

Я использую attr_encrypted в рельсах 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?

И это моя модель:

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 ответ

Решение

Просто предположение, но я думаю, что проблема в 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
Другие вопросы по тегам