# Problem with attr\_accessor

**URL:** <https://www.ruby-forum.com/t/problem-with-attr-accessor/194924>\
**Category:** Rails\
**Created:** [September 13, 2010, 9:18pm UTC](https://www.ruby-forum.com/t/problem-with-attr-accessor/194924 "2010-09-13T21:18:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 13, 2010, 9:18pm UTC](https://www.ruby-forum.com/t/problem-with-attr-accessor/194924/1 "2010-09-13T21:18:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 14, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/problem-with-attr-accessor/194924/2 "2010-09-14T15:32:26Z")

</div>

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](http://codosaur.us) | Work: [http://davearonson.com](http://davearonson.com)  
Leadership Blog: [http://dare2xl.com](http://dare2xl.com) | Play: [http://davearonson.net](http://davearonson.net)

- 
  - 
    - 
      - 
        - WATCH THIS SPACE \* \* \* \* \* | Ruby: [http://mars.groupsite.com](http://mars.groupsite.com)

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 14, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/problem-with-attr-accessor/194924/3 "2010-09-14T15:44:43Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 14, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/problem-with-attr-accessor/194924/4 "2010-09-14T16:03:47Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 14, 2010, 4:34pm UTC](https://www.ruby-forum.com/t/problem-with-attr-accessor/194924/5 "2010-09-14T16:34:07Z")

</div>

Dave A. wrote:

> On Tue, Sep 14, 2010 at 09:44, Marnen Laibow-Koser  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [September 14, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/problem-with-attr-accessor/194924/6 "2010-09-14T16:27:29Z")

</div>

On Tue, Sep 14, 2010 at 09:44, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://codosaur.us) | Work: [http://davearonson.com](http://davearonson.com)  
Leadership Blog: [http://dare2xl.com](http://dare2xl.com) | Play: [http://davearonson.net](http://davearonson.net)

- 
  - 
    - 
      - 
        - WATCH THIS SPACE \* \* \* \* \* | Ruby: [http://mars.groupsite.com](http://mars.groupsite.com)
