# Retrieving rows from polymorphically related tables

**URL:** <https://www.ruby-forum.com/t/retrieving-rows-from-polymorphically-related-tables/122651>\
**Category:** Rails\
**Created:** [December 10, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/retrieving-rows-from-polymorphically-related-tables/122651 "2007-12-10T12:05:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [December 10, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/retrieving-rows-from-polymorphically-related-tables/122651/1 "2007-12-10T12:05:47Z")

</div>

Hi,

I am new to ROR and ActiveRecord. I would like to present the following  
problem to you. Your help is appreciated and let me thank you in advance  
for all your support.

# I have the following three models created applying the polymorphic concept

class SoftwareCi \< ActiveRecord::Base  
has\_one :ci, :as =\> :content  
end

class HardwareCi \< ActiveRecord::Base  
has\_one :ci, :as =\> :content  
end

class Ci \< ActiveRecord::Base  
belongs\_to :content, :polymorphic =\> true  
end

# The table, cis ,contains the following records

id | ci\_number | content\_id | content\_type  
----±---------±----------±-----------±-------------  
1 | CI1 | 1 | SoftwareCi  
2 | CI2 | 2 | SoftwareCi  
3 | CI3 | 1 | HardwareCi  
4 | CI4 | 2 | HardwareCi

# The table, software\_cis ,contains the following records

id | asset\_tag | status | version  
----±------------------±--------±--------  
1 | AC Logix - AT1 | Status2 | 1.0  
2 | RR Logix AT2 | Status3 | 2.3

# The table, hardware\_cis ,contains the following records

id | asset\_tag | status | version  
----±------------------±--------±--------  
1 | HCL - AT1 | Status1 | 1.0  
2 | IBM - AT2 | Status1 | 2.3

# The controller, ci\_controller, contains the list method as follows

def list  
@cis=Ci.find(:all)  
end

# The rhtml file, list, contains the following code

\<% for ci in @cis %\>

\<% ci.ci\_number %\>  
\<% ci\_id=ci.content\_id %\>

\<% end %\>

# Now let me get into the help I need from you.

1. The above code perfectly prints the values of the attributes  
ci\_number and ci.content\_id from the table, cis.

2. I would like to know how I can print the values of attributes of rows  
from the software\_cis or hardware\_cis table along the attributes from  
the table, cis. A sample output i would like to print based on the  
content of tables I presented would be as follows

CI1, 1, SoftwareCi, AC Logix - AT1, Status2 , 1.0  
CI2, 2, SoftwareCi, RR Logix AT2, Status3 , 2.3  
CI3, 1, HardwareCi, HCL - AT1 , Status1 , 1.0  
CI4, 2, HardwareCi, IBM - AT2 , Status1 , 2.3

What are the changes I have to make in the code?

Expecting your reply at the earliest…thanks once again!

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [December 10, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/retrieving-rows-from-polymorphically-related-tables/122651/2 "2007-12-10T12:17:00Z")

</div>

On 10 Dec 2007, at 11:05, Venu Vallayil wrote:

> 

Same way as you would get at any association: ci.content

Fred

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [December 10, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/retrieving-rows-from-polymorphically-related-tables/122651/3 "2007-12-10T13:27:00Z")

</div>

Frederick C. wrote:

> On 10 Dec 2007, at 11:05, Venu Vallayil wrote:
> 
> > 
> 
> Same way as you would get at any association: ci.content
> 
> Fred

Hi Fred,

It works…cool…  
Many many thanks…  
I may actively post ROR questions as I am into a serious project on ROR

Regards  
Venu
