Reputation: 4693
I'm using attr_encrypted
in rails 3.2.13 to encrypt a column. For that, in my model I have the following:
attr_encrypted :social_security_no, :key => 'a secret key'
The app does not save neither social_security_no
nor encrypted_social_security_no
on database.
I also tried spectator-attr_encrypted
gem. But, now it's giving the following error:
/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)
So, is there any way to get rid of this problem? Or, is there a forked version of the gem which works fine with Rails 3.2.13 and Ruby 2.0.0?
And, this is my model:
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
Upvotes: 4
Views: 1060
Reputation: 13621
Just a guess, but i think the issue is in the validates_unqiueness_of. One of my co-workers solved this by writing his own validation against it.
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
Upvotes: 1