# ActiveRecord relationships problem

**URL:** <https://www.ruby-forum.com/t/activerecord-relationships-problem/144753>\
**Category:** Rails\
**Created:** [August 1, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-problem/144753 "2008-08-01T19:31:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [August 1, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-problem/144753/1 "2008-08-01T19:31:38Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [August 1, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-problem/144753/2 "2008-08-01T20:29:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [August 1, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-problem/144753/3 "2008-08-01T21:23:01Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [August 1, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-problem/144753/4 "2008-08-01T22:19:36Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![nodrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nodrew](https://www.ruby-forum.com/u/nodrew)\
**Post date:** [August 1, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-problem/144753/5 "2008-08-01T21:12:22Z")

</div>

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?
