I am having some difficulty correctly setting up the relationships between two ActiveRecord model. The DB tables look something like this: table scripts { id :integer description :text } table households { id :integer script_id :integer } There are a small number of scripts and a large number of households. Each household has exactly one script. I have a household but when I call household.Script.description I am not getting the correct result. household.Script is returning nil if I define the relationship as follows (there is definitely a matching record in the scripts table for household.script_id): class Script < ActiveRecord::Base{ has_many :Household } class Household < ActiveRecord::Base { belongs_to :Script } I get "Mysql::Error: Unknown column ''scripts.household_id'' in ''where clause'': SELECT * FROM `scripts` WHERE (scripts.household_id = 7820) LIMIT 1" if I define it as: class Script < ActiveRecord::Base{ has_many :Household } class Household < ActiveRecord::Base { has_one :Script } Any ideas? -- 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 -~----------~----~----~----~------~----~------~--~---
Drew Batshaw wrote:> I am having some difficulty correctly setting up the relationships > between two ActiveRecord model. The DB tables look something like this: > > table scripts { > id :integer > description :text > } > > table households { > id :integer > script_id :integer > } > > There are a small number of scripts and a large number of households. > Each household has exactly one script. > > I have a household but when I call household.Script.description I am not > getting the correct result. household.Script is returning nil if I > define the relationship as follows (there is definitely a matching > record in the scripts table for household.script_id): > > class Script < ActiveRecord::Base{ > has_many :Household > } > > class Household < ActiveRecord::Base { > belongs_to :Script > } > > I get "Mysql::Error: Unknown column ''scripts.household_id'' in ''where > clause'': SELECT * FROM `scripts` WHERE (scripts.household_id = 7820) > LIMIT 1" if I define it as: > > class Script < ActiveRecord::Base{ > has_many :Household > } > > class Household < ActiveRecord::Base { > has_one :Script > } > > Any ideas?Your second configuration is definitly wrong, Household should definitly have only one Script but the syntax should be like your first example. i don''t really know how you are getting the wrong results but i have some few ideas. 1. use undercase for the relations names (has_many :households, belongs_to :script). 2. If you cleared your database data, Maybe your database''s sequences did not restart and old id''s are being used. -- 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 -~----------~----~----~----~------~----~------~--~---
Elad Meidar wrote:> > 1. use undercase for the relations names (has_many :households, > belongs_to :script). > 2. If you cleared your database data, Maybe your database''s sequences > did not restart and old id''s are being used.Changing it to undercase did that job. Any idea why that is? -- 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 -~----------~----~----~----~------~----~------~--~---
That''s the way the associations work; they use the ''table-ized'' version of the Class. So if you had a class name of BlogPost, you might have: has_one :blog_post or has_many :blog_posts On Aug 1, 12:12 pm, Drew Batshaw <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Elad Meidar wrote: > > > 1. use undercase for the relations names (has_many :households, > > belongs_to :script). > > 2. If you cleared your database data, Maybe your database''s sequences > > did not restart and old id''s are being used. > > Changing it to undercase did that job. Any idea why that is? > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Steve Downey wrote:> That''s the way the associations work; they use the ''table-ized'' > version of the Class. > > So if you had a class name of BlogPost, you might have: > > has_one :blog_post > > or > > has_many :blog_posts > > On Aug 1, 12:12�pm, Drew Batshaw <rails-mailing-l...-ARtvInVfO7m5VldFQK4jKA@public.gmane.orgt>That is good to know but strange since it has never been an issue in the past when I have used the class name. -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---