Forum: Ruby on Rails Single Table Inheritance

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.
4d47944ad8359101bccb0a798cd9acf1?d=identicon&s=25 hugh (Guest)
on 2005-11-12 17:00
(Received via mailing list)
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
437ee3e09fe9539cdec6ba44153186cd?d=identicon&s=25 wemigh (Guest)
on 2005-11-12 17:05
(Received via mailing list)
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
This topic is locked and can not be replied to.