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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-10 11:16 UTC
Re: Retrieving rows from polymorphically related tables
On 10 Dec 2007, at 11:05, Venu Vallayil wrote:> > > 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 >Same way as you would get at any association: ci.content Fred> 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! > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Venu Vallayil
2007-Dec-10 12:27 UTC
Re: Retrieving rows from polymorphically related tables
Frederick Cheung wrote:> On 10 Dec 2007, at 11:05, Venu Vallayil wrote: > >> > Same way as you would get at any association: ci.content > > FredHi Fred, It works.....cool.... Many many thanks..... I may actively post ROR questions as I am into a serious project on ROR Regards Venu -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---