# Single Table Inheritance problem

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-problem/60161>\
**Category:** Rails\
**Created:** [May 10, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-problem/60161 "2006-05-10T19:36:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 10, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-problem/60161/1 "2006-05-10T19:36:20Z")

</div>

I have two tables I am using single table inheritance with: Page and  
Item. “Page” has many “Items”; “Item” belongs to “Page”.

Item Model:  
class Item \< ActiveRecord::Base  
end

class Article \< Item  
belongs\_to :page  
end

Page Model:  
class Page \< ActiveRecord::Base  
end

class Issue \< Page  
has\_many :articles  
end

In my controller, when I call @page.articles I am getting the following  
problem:  
Mysql::Error: #42S22Unknown column ‘items.issue\_id’ in ‘where clause’:  
SELECT \* FROM items WHERE (items.issue\_id = 4) AND ( (items.`type` =  
‘Article’ ) )

I see exactly where the snafu is ‘items.issue\_id’ should be  
‘items.PAGE\_id’ (caps for emphasis). I assume either I am setting up  
the model incorrectly, or I’m trying to do something that’s not  
supported. Everything else is exactly how I expect it.

Is there any way out of this hole?

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 10, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-problem/60161/2 "2006-05-10T19:46:30Z")

</div>

On 5/10/06, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In my controller, when I call @page.articles I am getting the following  
> Is there any way out of this hole?  
> When you declare “has\_many :articles”, you are expected to have a  
> foreign key in the articles table referring to the table backing the  
> ActiveRecord class you’re declaring it in. So, when you do so from  
> your Issue class (“has\_many :articles”), ActiveRecord expects to see  
> an “issue\_id” foreign key in your articles table. Nothing surprising  
> here.

–  
Bosko M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 10, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-problem/60161/3 "2006-05-10T19:46:35Z")

</div>

On 5/10/06, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In my controller, when I call @page.articles I am getting the following  
> Is there any way out of this hole?

The code you’ve pasted doesn’t match the description you wrote of it,  
unless I’m misunderstanding what you’re looking for.

If your column is called page\_id rather than issue\_id, you should put  
the has\_many line in the Page class, not the Issue class.

If only Issues should have many Articles, and not Pages, you can  
either rename the column, or specify its name in the has\_many call.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 10, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-problem/60161/4 "2006-05-10T19:51:28Z")

</div>

Wilson B. wrote:

> On 5/10/06, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> If only Issues should have many Articles, and not Pages, you can  
> either rename the column, or specify its name in the has\_many call.

Exactly what I’m trying to do, thanks! How can I specify the name in  
the has\_many? I’m a little new to this rails thing…

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 10, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-problem/60161/5 "2006-05-10T20:05:24Z")

</div>

On 5/10/06, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Wilson B. wrote:
> 
> > On 5/10/06, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > If only Issues should have many Articles, and not Pages, you can  
> > either rename the column, or specify its name in the has\_many call.
> 
> Exactly what I’m trying to do, thanks! How can I specify the name in  
> the has\_many? I’m a little new to this rails thing…

Take a look at the :foreign\_key option in the API docs:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

Also, you may want to take a look at this, if you need something more  
flexible down the road:  
[http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations)
