Single Table Inheritance


#1

Hello,

I’m trying to build out an app with a couple of tables (people,
contacts) using single table inheritance. The people table has the
type field and is for, clients, employees, managers etc. contacts
simply holds phone numbers and email numbers. I scaffolded out person
and contact and have created the basic files below. For the life of
me can’t create a related phone number or email.

in people_controller.rb I have (model below):

def create_phone
person = Person.find(params[:id])
Person.Phone.create
redirect_to :action => ‘edit’, :controller => ‘Phones’, :id =>
@phone
end

with the idea this can be called from clients or employees.

and I get undefined method `Phone’ for Person:Class

Also if the scaffolded CRUD methods exist in person will they not be
inherited by client? To get anything to work I had to scaffold client
out as well.

person.rb
class Person < ActiveRecord::Base
has_many :contacts
end

client.rb
class Client < Person
end

contact.rb
class Contact < ActiveRecord::Base
belongs_to :person
end

phone.rb
class Phone < Contact
end

Thanks in advance for any help,
Hugh


#2

Instead of Person.Phone.create, you should have:
person.phone.create

When you capitalize, Ruby assumes that you’re talking about the class
(Person and Phone) rather than an instance (person and phone).

– Will

On Sat, 12 Nov 2005 10:57:39 -0500, Hugh Kelsey