Retrieving rows from polymorphically related tables

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!

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

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

Fred

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs