macfizz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-24 14:28 UTC
Managing a 1:1 relationship
Bit of a noddy question this, but I am getting confused and would welcome some help. I have two classes, called Site and Theme. In my model, a Site has_one theme and a Theme belongs_to a site (and I set them up like that in my model files). In my migration files I put a column in the sites table for theme_id. Having created the controllers everything is starting to look OK on the scaffolding web pages. However, I wanted to check out the relationships in the console prior to adding code to pull the different models together, and thought that this would work:>> sitetest = Site.find 1[result OK]>> sitetest.name[result OK]>> sitetest.theme_id1 [that''s OK]>> sitetest.theme.nameActiveRecord::StatementInvalid: Mysql::Error: Unknown column ''themes.site_id'' in ''where clause'': SELECT * FROM themes WHERE (themes.site_id = 1) LIMIT 1 I thought that since a Site has a Theme, this should have worked. I''m now a little confused about whether my models are correct. I did wonder whether in fact I should have put a site_id column in my themes table instead of a theme_id column in my sites table, but it seems counter-intuitive. If someone could help me to clear the fog, I''d be very grateful. Regards, macfizz. -- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oops - except it''s not a 1:1 relationship after all. I screwed up. A Site has one Theme, but a Theme can be used in a whole range of Sites, so it is actually 1:many isn''t it? Sorry to waste your time. :-( macfizz. -- On 24 Sep 2006, at 3:28 pm, macfizz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > Bit of a noddy question this, but I am getting confused and would > welcome some help. > > I have two classes, called Site and Theme. In my model, a Site has_one > theme and a Theme belongs_to a site (and I set them up like that in my > model files). In my migration files I put a column in the sites table > for theme_id. Having created the controllers everything is starting to > look OK on the scaffolding web pages. However, I wanted to check out > the relationships in the console prior to adding code to pull the > different models together, and thought that this would work: > >>> sitetest = Site.find 1 > [result OK] >>> sitetest.name > [result OK] >>> sitetest.theme_id > 1 [that''s OK] >>> sitetest.theme.name > ActiveRecord::StatementInvalid: Mysql::Error: Unknown column > ''themes.site_id'' in ''where clause'': SELECT * FROM themes WHERE > (themes.site_id = 1) LIMIT 1 > > I thought that since a Site has a Theme, this should have worked. I''m > now a little confused about whether my models are correct. I did > wonder > whether in fact I should have put a site_id column in my themes table > instead of a theme_id column in my sites table, but it seems > counter-intuitive. > > If someone could help me to clear the fog, I''d be very grateful. > > Regards, > > > macfizz. > -- > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---