パーフェクトRailsの「コールバックをクラスに分離する」に載ってるコードは、
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html にある↓のコードです。
class BankAccount < ActiveRecord::Base before_save EncryptionWrapper.new("credit_card_number") after_save EncryptionWrapper.new("credit_card_number") after_initialize EncryptionWrapper.new("credit_card_number") end class EncryptionWrapper def initialize(attribute) @attribute = attribute end def before_save(record) record.send("#{@attribute}=", encrypt(record.send("#{@attribute}"))) end def after_save(record) record.send("#{@attribute}=", decrypt(record.send("#{@attribute}"))) end alias_method :after_initialize, :after_save private def encrypt(value) # Secrecy is committed end def decrypt(value) # Secrecy is unveiled end end
このBankAccountに、ちゃんと任意のカラムをつくり、モデルを作成しました。
そして、rails consoleで試すと、↓になりました。
> BankAccount NameError: uninitialized constant BankAccount::EncryptionWrapper
BankAccountのコードは、何がおかしいのでしょうか?
どうすれば、クラスのコールバックを試せるのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/18 05:13