# Single Table Inheritance Association Problem

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-association-problem/102713>\
**Category:** Rails\
**Created:** [March 12, 2007, 5:34pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-association-problem/102713 "2007-03-12T17:34:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 12, 2007, 5:34pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-association-problem/102713/1 "2007-03-12T17:34:51Z")

</div>

Hi there,

I’m using STI in a new application, and something is terribly wrong with  
has\_many associations.

Right, so I have number of subclasses that inherit from one concrete  
base class. Each sub class can possibly have associations via has\_many,  
or has\_one. When I try to access a collection on the sub class(  
subclass.items), I get 'undefined method ‘items’ for '. The  
strange thing is, accessing these subclasses in script/console works  
fine, and even works the first time I run the app in some cases. Once I  
refresh the page I get this error though.

I’m totally at a loss here. Any help would be greatly appreciated!

Thanks in advance.

Mike.

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 12, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-association-problem/102713/2 "2007-03-12T18:13:10Z")

</div>

It sounds like you do not have the type field set up properly. You  
could see this behavior if it was loading the target of the has\_many  
but instantiating the concrete class rather than the subclass. The  
message may be just reporting the class the association is targeted  
at. This is just a guess, but fits the description.

Michael

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 12, 2007, 6:53pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-association-problem/102713/3 "2007-03-12T18:53:24Z")

</div>

Thanks for the input Michael.

Well, the thing is, it shows the subclass name in the error message.  
Undefined method blah for \<# subclass name here \>.

MichaelLatta wrote:

> It sounds like you do not have the type field set up properly. You  
> could see this behavior if it was loading the target of the has\_many  
> but instantiating the concrete class rather than the subclass. The  
> message may be just reporting the class the association is targeted  
> at. This is just a guess, but fits the description.
> 
> Michael

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [March 12, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-association-problem/102713/4 "2007-03-12T20:06:40Z")

</div>

Alright, we got it working. Here’s the deal:

We had it setup originally with all the subclasses in one file, and we  
were requiring that in Application.rb. Once I pulled each subclass out,  
and put it in its own file it took right off.

Mike wrote:

> Thanks for the input Michael.
> 
> Well, the thing is, it shows the subclass name in the error message.  
> Undefined method blah for \<# subclass name here \>.
> 
> MichaelLatta wrote:
> 
> > It sounds like you do not have the type field set up properly. You  
> > could see this behavior if it was loading the target of the has\_many  
> > but instantiating the concrete class rather than the subclass. The  
> > message may be just reporting the class the association is targeted  
> > at. This is just a guess, but fits the description.
> > 
> > Michael

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [January 7, 2008, 12:36am UTC](https://www.ruby-forum.com/t/single-table-inheritance-association-problem/102713/5 "2008-01-07T00:36:54Z")

</div>

I’m having a similar problem, but it’s just a case of a method not  
being inherited. In my particular situation, I have a Class and  
various Subclasses, all on the ‘classes’ table. I find that class  
methods on my base Class (i.e. defined with def self.methodname)  
inherit to the Subclasses just fine, but object methods (i.e. defined  
with def methodname) do not get inherited.

That may not have been clear. What I have is a class called Task,  
with subclasses PhonecallTask and SendemailTask. In the ‘tasks’ table  
I have a field called ‘stringified\_params’, which is a hash that has  
been stringified. I have a function called ‘parameters’ in the Task  
class, as follows:

def parameters  
eval(stringified\_params)  
end

When I try to call my\_phonecall\_task.parameters I’m expecting a hash  
but I get an exception: undefined method `parameters’ for  
#PhonecallTask:0x3215cac

This only happens when attempting access via the web; in the console I  
can read a PhonecallTask from the database and then call the  
‘parameters’ method just fine.

What’s going on? I’m using rails REV 6042.

Thanks - Luke

On Mar 12 2007, 2:06 pm, Mike [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
