I''m using rails to build a web-accessible database that will store
information about patients that come into our hospital with brain
injuries. Here''s my problem:
I''m trying to call a belongs_to relationship between two models, one
of which descends from another using single-table inheritance. The
first model is Patient:
class Patient < ActiveRecord::Base
belongs_to :race, :class_name => "PropertyList"
belongs_to :sex, :class_name => "PropertyList"
end
The second is PropertyList - all of these class definitions are in
the same file:
class PropertyList < ActiveRecord::Base
def description
form_number.to_s + ": " + name
end
end
class Race < PropertyList
has_many :patients
end
class Sex < PropertyList
has_many :patients
end
When I attempt to list a patient record and do this:
patient.race.name
I get "undefined method ''race'' for
#<Patient>"... I know that the
race_id and sex_id in the patients table (the foreign keys for this
relationship) are not null. Also, when I fire up script/console and
attempt:
races = Race.find(:all)
I get "NameError: uninitialized constant Races". Strangely, doing
races = PropertyList.find_all_by_type("Race")
it works just fine. It seems I don''t understand how relationships
work, or there''s some STI weirdness that''s going on.
I''ve read the
API docs several times, searched the list archives and googled to no
avail.
Any help that anyone could provide would be greatly appreciated.
Thanks -
Holland Alday
Medical Scientist Training Program
University of Mississippi Medical Center
holland-0lHqbOJ5DwTR7s880joybQ@public.gmane.org
Clarification of my previous question:
Here''s my situation: I''ve got an AR class called PropertyList.
It has
classes derived from it that include Race and Sex (this is a database
of patients), like this:
class PropertyList < ActiveRecord::Base; end
class Race < PropertyList; end
Typing PropertyList.find(:all) at script/console returns everything
in the table, like it should. Typing Race.find(:all) gives:
NameError: uninitialized constant Race
...almost as if the class had never been declared. I can''t figure out
what''s going on. Any help that anyone could provide would be greatly
appreciated.
- Holland
PS - the AR::Base API documentation on single-table inheritance
(http://api.rubyonrails.com/classes/ActiveRecord/Base.html) gives an
example where Firm is set up as a subclass of Company. Fetching this
record from the db is done by calling Company.find, not Firm.find. I
feel sure there is a reason for this, but I can''t tell if this is a
limitation of AR or if I''m just clueless.
On 8/9/05, Holland <holland@bipedal.net> wrote:> NameError: uninitialized constant RaceI get the same error with single table inheritance model with rails 1.0 Isn''t there any planned fix ?
J?r?me L wrote:> On 8/9/05, Holland <holland@bipedal.net> wrote: > >> NameError: uninitialized constant Race > > I get the same error with single table inheritance model with rails 1.0 > Isn''t there any planned fix ?In the dim depths of my memory (must be going back, ooh, weeks), I seem to recall a similar problem that was fixed by making sure that the interpreter had seen the STI subclasses before anything else happened, because there''s a set of conditions that result in them not getting loaded automatically. I may be completely misremembering, but I *think* putting a require ''stifile'' for each STI subclass file at the end of boot.rb does the trick. Perhaps someone with a less sieve-like memory can pitch in and correct me here? -- Alex
Jérôme L
2006-Feb-09 09:42 UTC
[Rails] [partly RESOLVED] ''undefined method'' using belongs_to and STI
On 2/3/06, Alex Young <alex@blackkettle.org> wrote:> In the dim depths of my memory (must be going back, ooh, weeks), I seem > to recall a similar problem that was fixed by making sure that the > interpreter had seen the STI subclasses before anything else happened, > because there''s a set of conditions that result in them not getting > loaded automatically. I may be completely misremembering, but I *think* > putting a require ''stifile'' for each STI subclass file at the end of > boot.rb does the trick. > > Perhaps someone with a less sieve-like memory can pitch in and correct > me here?Putting `model :sti` in application.rb does also the trick (it would be a more elegant issue)