# STI and foreign\_key problem

**URL:** <https://www.ruby-forum.com/t/sti-and-foreign-key-problem/57096>\
**Category:** Rails\
**Created:** [April 7, 2006, 5:13am UTC](https://www.ruby-forum.com/t/sti-and-foreign-key-problem/57096 "2006-04-07T05:13:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 7, 2006, 5:13am UTC](https://www.ruby-forum.com/t/sti-and-foreign-key-problem/57096/1 "2006-04-07T05:13:36Z")

</div>

Hi,

I am trying to use STI since my classes only vary by one column in the  
database.  
Here is the structure (names have been changed to protect the innocent):

```
  Case
/ \

```

Registered Initial  
\ /  
Children

A registered case has additional information than an initial case (I  
realize I could just set a flag to separate them, but there is a good  
reason to separate them), but both need to be related to the children  
table. Here is the model code:

class Case \< ActiveRecord::Base  
end

class Registered \< Case  
has\_many :children, :foreign\_key =\> “case\_id”  
end

class Initial \< Case  
has\_many :children, :foreign\_key =\> “case\_id”  
end

class Child \< ActiveRecord::Base  
belongs\_to :initial  
belongs\_to :registered  
end

When I attempt to access the children attribute via an Initial object I  
get an unknown column exception and the dump shows the sql is trying to  
select “initial\_id” from the children table. This despite the fact I  
specified the foreign key of ‘case\_id’ in the Initial model.

This has been driving me crazy this evening. All searches for help have  
come up empty. Anyone help would be greatly appreciated.

Thanks!

Peer

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 7, 2006, 6:00am UTC](https://www.ruby-forum.com/t/sti-and-foreign-key-problem/57096/2 "2006-04-07T06:00:03Z")

</div>

Hi Peer,

With your structure you can add the line  
has\_many :children, :foreign\_key =\> “case\_id”  
to the case definition.

The reason that the sql is looking for the initial\_id is because the  
class  
Child belongs to it.

There are 3 options,

1. Include an initial\_id and a registered\_id to your children table  
If there is a requirement that it belongs to an inital or registered  
case  
only include that in the validation of the child object.

2. Include a case\_id in the Child table and change the Child class  
definition to:  
class Child \< ActiveRecord::Base  
belongs\_to :initial, :foreign\_key =\> ‘case\_id’  
belongs\_to :registered, :foreign\_key =\> ‘case\_id’  
end

I don’t know the wisdom of doing this one. Having never tried it it may  
milage may be non-existant.

1. Make Child polymorphic. Then you can associate children with  
anything.

I would tend to go for option 3. It’s cleaner and allows for the  
future.

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 7, 2006, 6:03am UTC](https://www.ruby-forum.com/t/sti-and-foreign-key-problem/57096/3 "2006-04-07T06:03:10Z")

</div>

Sorri to repost so soon,

Looking at your associations, you already have the case\_id in the child  
table.

With STI initial and registered _ARE_ cases. So you can just use the  
case\_id. When you query it you can decide.

child.case.type will tell you what class it is.

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 7, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/sti-and-foreign-key-problem/57096/4 "2006-04-07T15:00:38Z")

</div>

Peer A. wrote:

> Daniel ----- wrote:
> 
> > Sorri to repost so soon,
> > 
> > Looking at your associations, you already have the case\_id in the child  
> > table.
> > 
> > With STI initial and registered _ARE_ cases. So you can just use the  
> > case\_id. When you query it you can decide.
> > 
> > child.case.type will tell you what class it is.
> 
> This is how I thought it was designed to work and how I thought I had  
> the classes configured to work just like you described here. Am I  
> missing something?
> 
> Peer

I figured out what I was doing wrong and I don’t think I want to mention  
it in public. But since some nuby (like me) might do the same thing in  
the future I guess it is only right that I do.

When I initially made the system I made separate models for Initial and  
Registered then I tried to switch it to STI. I know now that I setup up  
the STI correclty. However, I redeclared my classes, Initial and  
Registered, in the new case.rb file and did not delete the old model  
files. Parts of it were working so I thought everything was alright,  
but I was still using the old models that weren’t configured properly  
and hence the errors. Once I removed the old files and added the link  
to the case model in application.rb everything worked just fine.  
Whoops!

Thanks for listening (and helping Daniel!)

Peer

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [April 7, 2006, 1:18pm UTC](https://www.ruby-forum.com/t/sti-and-foreign-key-problem/57096/5 "2006-04-07T13:18:51Z")

</div>

Daniel ----- wrote:

> Sorri to repost so soon,
> 
> Looking at your associations, you already have the case\_id in the child  
> table.
> 
> With STI initial and registered _ARE_ cases. So you can just use the  
> case\_id. When you query it you can decide.
> 
> child.case.type will tell you what class it is.

This is how I thought it was designed to work and how I thought I had  
the classes configured to work just like you described here. Am I  
missing something?

Peer
