ActiveRecord relationships problem

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?

Drew B. 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.

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 B. [email protected]

Steve D. 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 B. [email protected]

That is good to know but strange since it has never been an issue in the
past when I have used the class name.

Elad M. 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?