2013-04-06 47 views
4

我在rails 3.2.13中使用attr_encrypted來加密列。對於這一點,在我的模型,我有以下:RoR:attr_encrypted不在數據庫中保存數據

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

應用程序不保存既不social_security_no也不encrypted_social_security_no數據庫。我也試過spectator-attr_encrypted寶石。但是,現在它給了以下錯誤:

/home/ashish/.rvm/gems/[email protected]/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 
+0

請添加完整的model.rb文件。 – wintermeyer 2013-04-06 19:38:41

+0

謝謝!我更新了問題,請參考模型代碼的問題。 – 2013-04-07 11:41:31

回答

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