# Identifying the attributes of an object dynamically

**URL:** <https://www.ruby-forum.com/t/identifying-the-attributes-of-an-object-dynamically/142376>\
**Category:** Rails\
**Created:** [July 7, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/identifying-the-attributes-of-an-object-dynamically/142376 "2008-07-07T13:29:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@shankar](https://www.ruby-forum.com/u/shankar)\
**Post date:** [July 7, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/identifying-the-attributes-of-an-object-dynamically/142376/1 "2008-07-07T13:29:20Z")

</div>

Hello

I have an object ‘details’ which comprises of many attributes like

name  
address.city  
address.state  
department.name

For a given object of ‘details’, I am trying to obtain the values of  
some attributes from a given list (“name”,"department.name "), say from  
the config. I tried iterating over the list and access the object’s  
attrib using instance\_variable\_get, but ended up with the error

([“name”] is not a symbol)

Is there something incorrect that I am trying to do ?

~Shankar.

---

<div class="post-metadata">

**Author:** ![shankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@shankar](https://www.ruby-forum.com/u/shankar)\
**Post date:** [July 7, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/identifying-the-attributes-of-an-object-dynamically/142376/2 "2008-07-07T13:41:40Z")

</div>

On 7 Jul 2008, at 12:29, Shankar N. wrote:

> For a given object of ‘details’, I am trying to obtain the values of  
> some attributes from a given list (“name”,"department.name "), say  
> from  
> the config. I tried iterating over the list and access the object’s  
> attrib using instance\_variable\_get, but ended up with the error

Assuming you are talking about an ActiveRecord object, database  
attributes are not instance variables. attributes surprisingly enough  
contains all the attributes.

Fred

---

<div class="post-metadata">

**Author:** ![shankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@shankar](https://www.ruby-forum.com/u/shankar)\
**Post date:** [July 7, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/identifying-the-attributes-of-an-object-dynamically/142376/3 "2008-07-07T13:46:56Z")

</div>

Frederick C. wrote:

> Assuming you are talking about an ActiveRecord object, database  
> attributes are not instance variables. attributes surprisingly enough  
> contains all the attributes.
> 
> Fred

Ah! yes… thanks for pointing that out, Fred. But is there something  
like ‘attributes\_get’ that I should be using to get the values of the  
attribute in my case ?

~Shankar.

---

<div class="post-metadata">

**Author:** ![shankar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@shankar](https://www.ruby-forum.com/u/shankar)\
**Post date:** [July 7, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/identifying-the-attributes-of-an-object-dynamically/142376/4 "2008-07-07T14:46:00Z")

</div>

> Ah! yes… thanks for pointing that out, Fred. But is there something  
> like ‘attributes\_get’ that I should be using to get the values of the  
> attribute in my case ?
> 
> ~Shankar.

Aww… was really a very dumb question to ask… ☹

object.attributes[\<attr\_name\>] would obviously give the value…

Thanks for the help, Fred. Appreciate it !!

~Shankar.
