Forum: Ruby on Rails Single Table Inheritance Association Problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike (Guest)
on 2007-03-12 18:34
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 <Subclass>'. 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.
MichaelLatta (Guest)
on 2007-03-12 19:13
(Received via mailing list)
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
Mike (Guest)
on 2007-03-12 19:53
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
Mike (Guest)
on 2007-03-12 21:06
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
Lucius (Guest)
on 2008-01-07 01:36
(Received via mailing list)
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>
This topic is locked and can not be replied to.