11155
(-- --)
1
I pass a value from controller to model with attr_accessor. before_save
works fine but not after_find. What could be wrong?
This is what I use in index
@test = Test.find(:all)
@test.each {|a| a.key = cookies[:thekey]}
and this in create, show, edit:
@test.key = cookies[:thekey]
This is my model:
attr_accessor :key
after_find :decrypt_data
define_method(:after_find) { }
before_save :crypt_data
private
def crypt_data
self.name = Crypto.encrypt(self.name,key)
end
def decrypt_data
self.name = Crypto.decrypt(self.name,key)
end
Pål Bergström wrote:
def crypt_data
self.name = Crypto.encrypt(self.name,key)
end
def decrypt_data
self.name = Crypto.decrypt(self.name,key)
end
Are you sure you don't mean @name? This is Ruby, not Python, so self
would generally refer to the class or module, not the object.
-Dave
11155
(-- --)
3
Dave Aronson wrote:
P�l Bergstr�m wrote:
def crypt_data
� � �self.name = Crypto.encrypt(self.name,key)
end
def decrypt_data
� � �self.name = Crypto.decrypt(self.name,key)
end
Are you sure you don't mean @name? This is Ruby, not Python, so self
would generally refer to the class or module, not the object.
That's not true. In an instance method, self refers to the object.
-Dave
Best,
11155
(-- --)
4
Pål Bergström wrote:
I pass a value from controller to model with attr_accessor. before_save
works fine but not after_find. What could be wrong?
Note this sentence from the documentation:
"Unlike all the other callbacks, after_find and after_initialize will
only be run if an explicit implementation is defined (def after_find)."
Try that.
Best,
Oops, you're right, on further research it seems to be a slippery
little bugger. No wonder I usually see people using @ (or @@)
instead....
-Dave
11155
(-- --)
6
Dave Aronson wrote:
That's not true. �In an instance method, self refers to the object.
Oops, you're right, on further research it seems to be a slippery
little bugger.
Not slippery at all. It's very well defined.
No wonder I usually see people using @ (or @@)
instead....
That has a completely different meaning.
-Dave
Best,