Problem with attr_accessor

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


Specialization is for insects. -RAH | Have Pun, Will Babble! -me
Programming Blog: http://codosaur.us | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net

Dave A. 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Dave A. wrote:

On Tue, Sep 14, 2010 at 09:44, Marnen Laibow-Koser
[email protected] 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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Tue, Sep 14, 2010 at 09:44, Marnen Laibow-Koser
[email protected] 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. No wonder I usually see people using @ (or @@)
instead…

-Dave


Specialization is for insects. -RAH | Have Pun, Will Babble! -me
Programming Blog: http://codosaur.us | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs