Forum: Ruby on Rails Single-table inheritance: Uninitialized class constant

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.
06fa952559609d00a12ad22d74335282?d=identicon&s=25 Fritz Anderson (fritza)
on 2008-11-11 22:36
This forum has been of no help before, but I am nothing daunted.

I have a "people" table, mapping to a Person ActiveRecord class, which
has two subclasses, Principal and Secondary. There is a type column.

There is a "locations" table, mapping to Location. Location belongs_to
Principal. Principal has_many Locations. There is a principal_id column
in locations.

There is a LocationsController. It renders rhtml, which contains
something like

<%= @location.principal.full_name %>

... to which Rails complains "uninitialized constant
Location::Principal". locations_controller.rb does not refer to
Principal. The problem must come from the class information in the
relationship, or possibly in the STI type information.

If I change the belongs_to to:

belongs_to :principal, :class_name => "Person", :foreign_key =>
"principal_id"

it works, but this bothers me. It is not what I intend. Locations belong
to Principals, not to People in general.

The alternative, I think, is to have locations_controller.rb require
'person', but that gives me problems with reloading in development mode.

What should I do?

$ ruby --version
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
$ rails --version
Rails 1.2.6
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-11 22:52
(Received via mailing list)
On 11 Nov 2008, at 21:36, Fritz Anderson wrote:
>
> The alternative, I think, is to have locations_controller.rb require
> 'person', but that gives me problems with reloading in development
> mode.
>
use require_dependency instead of require (and i would do this at the
top of the location model, not in the controller)

Fred
06fa952559609d00a12ad22d74335282?d=identicon&s=25 Fritz Anderson (fritza)
on 2008-11-12 21:18
Frederick Cheung wrote:
> use require_dependency instead of require (and i would do this at the
> top of the location model, not in the controller)

This was exactly what I needed. Thank you.

It's too bad that Pickaxe (2nd ed) and Agile Web Development with Rails
(2nd ed) didn't mention require_dependency. Can anyone recommend a
reference that would have included it?

    — F
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-12 23:37
(Received via mailing list)
On Nov 12, 8:18 pm, Fritz Anderson <rails-mailing-l...@andreas-s.net>
wrote:
> Frederick Cheung wrote:
> > use require_dependency instead of require (and i would do this at the
> > top of the location model, not in the controller)
>
> This was exactly what I needed. Thank you.
>
> It's too bad that Pickaxe (2nd ed) and Agile Web Development with Rails
> (2nd ed) didn't mention require_dependency. Can anyone recommend a
> reference that would have included it?
>
Well it's a rails only thing so the Pickaxe would never cover it and
it's also a bit of slightly hairy internal stuff. "Luke, Use the
source!"

Fred
This topic is locked and can not be replied to.