# Access an Objects attribute as a string with HABTM

**URL:** <https://www.ruby-forum.com/t/access-an-objects-attribute-as-a-string-with-habtm/153262>\
**Category:** Rails\
**Created:** [November 14, 2008, 2:29am UTC](https://www.ruby-forum.com/t/access-an-objects-attribute-as-a-string-with-habtm/153262 "2008-11-14T02:29:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mernagh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@Mernagh](https://www.ruby-forum.com/u/Mernagh)\
**Post date:** [November 14, 2008, 2:29am UTC](https://www.ruby-forum.com/t/access-an-objects-attribute-as-a-string-with-habtm/153262/1 "2008-11-14T02:29:21Z")

</div>

I have searched many sites for the solution. And maybe there is none  
or more likely it is so easy no one mentions it.

The problem is this. I have an class called Event which has a HABTM  
relationship with Note.

I can do the following with no problems  
e = Event.new  
e[‘name’] = ‘test’  
e[‘state’] = ‘started’

But I cannot access the notes attribute in the same way  
e.notes \<\< Note.find(:all)  
e.notes.size #=\> 11  
e[‘notes’] #=\> nil

Why is it I can access e.name as e[‘name’] but not e.notes as e  
[‘notes’]?

And is there a work around to access notes only knowing the string  
name (“notes”)?

Will

---

<div class="post-metadata">

**Author:** ![Mernagh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@Mernagh](https://www.ruby-forum.com/u/Mernagh)\
**Post date:** [November 14, 2008, 3:25pm UTC](https://www.ruby-forum.com/t/access-an-objects-attribute-as-a-string-with-habtm/153262/2 "2008-11-14T15:25:11Z")

</div>

Thanks Fred.  
I had a look at the send method. That is exactly what I needed. Also  
thanks for not giving the answer in code since I would probably not  
have read up on the send method and fully understood it.

A good explanation of the send method for anyone else that wanders  
here is  
[http://www.softiesonrails.com/2007/8/15/ruby-101-methods-and-messages](http://www.softiesonrails.com/2007/8/15/ruby-101-methods-and-messages)

and the API is defined here  
[http://ruby-doc.org/core/classes/Object.html#M000334](http://ruby-doc.org/core/classes/Object.html#M000334)

Will

On Nov 13, 8:30 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Mernagh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@Mernagh](https://www.ruby-forum.com/u/Mernagh)\
**Post date:** [November 14, 2008, 2:31am UTC](https://www.ruby-forum.com/t/access-an-objects-attribute-as-a-string-with-habtm/153262/3 "2008-11-14T02:31:07Z")

</div>

On Nov 13, 9:29 pm, Mernagh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But I cannot access the notes attribute in the same way  
> e.notes \<\< Note.find(:all)  
> e.notes.size #=\> 11  
> e[‘notes’] #=\> nil
> 
> Why is it I can access e.name as e[‘name’] but not e.notes as e  
> [‘notes’]?

Because name is an attribute (ie backed by some column on your table),  
but notes isn’t (its an association). The [] and []= methods are  
equivalent to read/write\_attribute, which as their names suggest are  
for playing with attributes.

> And is there a work around to access notes only knowing the string  
> name (“notes”)?

Read up on your ruby (in particular the send method)

Fred
