Ali Williams
2006-Jul-13 23:32 UTC
[Rails] Relationships: one model referencing same table for different data sets?
Hi, After I''ve saved, I want to display the Description from the Codes table that corresponds with the ID in the Cause table. Rails doesn''t seem to like that, because on my form, I have two sets of data in combo boxes that come from the Codes table. So, in the show template, I can''t do this: Cause of Death: <%= @cause.code.description %> Any ideas? The database I''m converting from had all the code tables broken out, but I wanted to combine them all into one code table. There must be some way to display the text instead of the id!! Any and all help greatly appreciated! Ali
Brian Hogan
2006-Jul-13 23:52 UTC
[Rails] Relationships: one model referencing same table for different data sets?
Lots of ideas, but I''d need to see your migrations / table layouts and your models... there''s not enough here to go on. On 7/13/06, Ali Williams <awilliams@washoecounty.us> wrote:> > Hi, > > After I''ve saved, I want to display the Description from the Codes table > that > corresponds with the ID in the Cause table. > > Rails doesn''t seem to like that, because on my form, I have two sets of > data in > combo boxes that come from the Codes table. So, in the show template, I > can''t > do this: > > Cause of Death: <%= @cause.code.description %> > > Any ideas? > > The database I''m converting from had all the code tables broken out, but I > wanted to combine them all into one code table. There must be some way to > display the text instead of the id!! > > Any and all help greatly appreciated! > > Ali > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/1ff7d343/attachment.html
Ali Williams
2006-Jul-14 19:42 UTC
[Rails] Re: Relationships: one model referencing same table for different data sets?
Does this help? Please let me know - I am pulling my hair out over this. Thank you!! class Cause < ActiveRecord::Base has_many :codes end class Code < ActiveRecord::Base belongs_to :codetype belongs_to :cause end -- -- Table structure for table `causes` -- CREATE TABLE `causes` ( `id` int(11) NOT NULL auto_increment, `case_id` int(11) NOT NULL default ''0'', `cod_id` int(11) NOT NULL default ''0'',(code.codetype_id where codetype_id = 4) `mod_id` int(11) default ''0'',(code.codetype_id where codetype_id = 8) `cause_texta` varchar(255) default NULL, `cause_textb` varchar(120) default NULL, `cause_textc` varchar(120) default NULL, `cause_textd` varchar(150) default NULL, `date_entered` datetime NOT NULL default ''0000-00-00 00:00:00'', `entered_by` varchar(15) NOT NULL default '''', PRIMARY KEY (`id`), KEY `cod_id` (`cod_id`), KEY `case_id` (`case_id`) ) TYPE=MyISAM AUTO_INCREMENT=31 ; -- -- Table structure for table `codes` -- CREATE TABLE `codes` ( `id` int(11) NOT NULL auto_increment, `codetype_id` int(11) NOT NULL default ''0'', `abbreviation` varchar(20) default NULL, `description` varchar(50) default NULL, `other` varchar(35) default NULL, `obsolete_date` date default NULL, PRIMARY KEY (`id`), KEY `codetype_id` (`codetype_id`) ) TYPE=MyISAM AUTO_INCREMENT=317 ;
Brian Hogan
2006-Jul-14 20:27 UTC
[Rails] Re: Relationships: one model referencing same table for different data sets?
Well.. it''s not gonna work because your tables aren''t following the conventions. You have no relationship that Rails can understand in your table How can I explain this.... What you have here is just not going to work. If you want a cause to have many codes, then the foreign key needs to be in the other table... the codes table needs to have the cause_id column. If you want to have the code_id in the cause table, it needs to be called code_id Then you''d be able to do class Cause < ActiveRecord::Base belongs_to :code end Does a cause have multiple codes? If it does, then you need to create a causes_codes table and use has_and_belongs_to_many causes_codes === cause_id code_id class Cause < ActiveRecord::Base has_and_belongs_to_many :codes end class Code < ActiveRecord::Base belongs_to :codetype has_and_belongs_to_many :causes end Need more help? let me know. On 7/14/06, Ali Williams <awilliams@washoecounty.us> wrote:> > Does this help? Please let me know - I am pulling my hair out over > this. Thank > you!! > > class Cause < ActiveRecord::Base > has_many :codes > end > > class Code < ActiveRecord::Base > belongs_to :codetype > belongs_to :cause > end > > -- > -- Table structure for table `causes` > -- > > CREATE TABLE `causes` ( > `id` int(11) NOT NULL auto_increment, > `case_id` int(11) NOT NULL default ''0'', > `cod_id` int(11) NOT NULL default ''0'',(code.codetype_id where > codetype_id = 4) > `mod_id` int(11) default ''0'',(code.codetype_id where codetype_id = 8) > `cause_texta` varchar(255) default NULL, > `cause_textb` varchar(120) default NULL, > `cause_textc` varchar(120) default NULL, > `cause_textd` varchar(150) default NULL, > `date_entered` datetime NOT NULL default ''0000-00-00 00:00:00'', > `entered_by` varchar(15) NOT NULL default '''', > PRIMARY KEY (`id`), > KEY `cod_id` (`cod_id`), > KEY `case_id` (`case_id`) > ) TYPE=MyISAM AUTO_INCREMENT=31 ; > > -- > -- Table structure for table `codes` > -- > > CREATE TABLE `codes` ( > `id` int(11) NOT NULL auto_increment, > `codetype_id` int(11) NOT NULL default ''0'', > `abbreviation` varchar(20) default NULL, > `description` varchar(50) default NULL, > `other` varchar(35) default NULL, > `obsolete_date` date default NULL, > PRIMARY KEY (`id`), > KEY `codetype_id` (`codetype_id`) > ) TYPE=MyISAM AUTO_INCREMENT=317 ; > > > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060714/36209543/attachment.html
Ali Williams
2006-Jul-14 20:39 UTC
[Rails] Re: Relationships: one model referencing same table for different data sets?
Ok, I get what you''re saying here. Cause does have multiple codes, BUT they are of two different CODE TYPES: cause of death and manner of death. So what you''re saying about adding a code_id to the cause table makes sense, but I''m working with two different data sets, so how can I differentiate between the two(cod_id, mod_id)? Maybe I need to concede to the fact that my code tables need to be broken out so I can property conform to what Rails wants.
Brian Hogan
2006-Jul-14 20:58 UTC
[Rails] Re: Relationships: one model referencing same table for different data sets?
No that''s easy then class Cause < ActiveRecord::Base belongs_to :cause_of_death, :class_name => "Code", :foreign_key => "cod_id" belongs_to :manner_of_death, :class_name => "Code", :foreign_key => "mod_id" end @cause = Cause.find :first @cause.manner_of_death.description @cause.cause_of_death.description On 7/14/06, Ali Williams <awilliams@washoecounty.us> wrote:> > Ok, I get what you''re saying here. Cause does have multiple codes, BUT > they are > of two different CODE TYPES: cause of death and manner of death. So what > you''re > saying about adding a code_id to the cause table makes sense, but I''m > working > with two different data sets, so how can I differentiate between the > two(cod_id, > mod_id)? Maybe I need to concede to the fact that my code tables need to > be > broken out so I can property conform to what Rails wants. > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060714/35ec0d67/attachment.html