# Rails Associations with dynamic attributes

**URL:** <https://www.ruby-forum.com/t/rails-associations-with-dynamic-attributes/146182>\
**Category:** Rails\
**Created:** [August 18, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/rails-associations-with-dynamic-attributes/146182 "2008-08-18T20:19:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sharad\_Gupta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecae2f/32.png) [@Sharad\_Gupta](https://www.ruby-forum.com/u/Sharad_Gupta)\
**Post date:** [August 18, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/rails-associations-with-dynamic-attributes/146182/1 "2008-08-18T20:19:42Z")

</div>

Hopefully, the table structure at the end is self-explanatory:

I want to be able to do something like this:  
@panelist = Panelist.first  
puts @panelist.age  
$stdout\> 30

puts @panelist.gender  
$stdout\> M

Is this doable through OTB Rails associations (polymorphic or  
otherwise), Or  
will I have to do some custom coding in order to achieve this?

Thanks

mysql\> select \* from panel\_attribute;  
±-------------------±---------------------+  
| panel\_attribute\_id | panel\_attribute\_name |  
±-------------------±---------------------+  
| 1 | age |  
| 2 | gender |  
±-------------------±---------------------+  
2 rows in set (0.00 sec)

mysql\> select \* from panelist;  
±------------±--------------+  
| panelist\_id | panelist\_name |  
±------------±--------------+  
| 1 | user1 |  
| 2 | user2 |  
±------------±--------------+  
2 rows in set (0.00 sec)

mysql\> select \* from panelist\_attribute;  
±----------------------±-------------------------±------------±-------------------+  
| panelist\_attribute\_id | panelist\_attribute\_value | panelist\_id |  
panel\_attribute\_id |  
±----------------------±-------------------------±------------±-------------------+  
| 1 | 30 | 1  
| 1 |  
| 2 | M | 1  
| 2 |  
| 3 | 31 | 2  
| 1 |  
| 4 | F | 2  
| 2 |  
±----------------------±-------------------------±------------±-------------------+  
4 rows in set (0.00 sec)

mysql\>

---

<div class="post-metadata">

**Author:** ![Sharad\_Gupta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecae2f/32.png) [@Sharad\_Gupta](https://www.ruby-forum.com/u/Sharad_Gupta)\
**Post date:** [August 18, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/rails-associations-with-dynamic-attributes/146182/2 "2008-08-18T20:26:11Z")

</div>

On 18 Aug 2008, at 18:49, Sharad Gupta wrote:

> Is this doable through OTB Rails associations (polymorphic or  
> otherwise), Or will I have to do some custom coding in order to  
> achieve this?

Well if panelist has\_many panelist\_attributes then you could easily do  
something like

def named\_attribute(name)  
attribute = panelist\_attributes.detect {|a|  
a.panel\_attribute.panel\_attribute\_name == name}  
attribute ? attribute.panelist\_attribute\_value : nil  
end

You could then hook that through method missing.

You get this to play nicely to rails you should probably rename you  
columns to fit the rails conventions. You’ll also save a lot of typing  
if you don’t ‘namespace’ your column names.  
the above is rather less verbose without these prefixes eg

def named\_attribute(name)  
attribute = attributes.detect {|a| a.attribute.name == name}  
attribute ? attribute.value : nil  
end
