Models and Inheritance


#1

I have a table of companies and a join table forming the relationship
between a list of possible services they can provide.

Working from a basic understanding of OOP I assumed that there should be
a series of Serviceprovider child objects of the company class.

Company < ActiveRecord::Base
has_belongs_to_many :services
end

Serviceprovidertype1 < Company

Serviceprovidertype2 < Company

These child classes have corresponding child controllers

Serviceprovidertype1Controller < CompanyController

in the parent I have a method

protected
list_by_service(service)
Company.find_by_sql(… + service + …)
end

and in the child Serviceprovidertype1Controller

list
list_by_service(‘Servicetype’)
end

but it only seems to work if I make the method in the parent a class
method ie

Company.list_by_service(service)

I don’t understand why the child just doesn’t inherit the parent method,

and why a class method should - I’ve read it’s used when you want it
to apply the same to all object instances ie

count_the_number of companies

Can anyone explain what I’ve obviously misunderstook, is my appraoch
using inheritance in this manner inappropriate ?

_tony


#2

and in the child Serviceprovidertype1Controller

list
list_by_service(‘Servicetype’)
end

but it only seems to work if I make the method in the parent a class
method ie

Company.list_by_service(service)

I think my problem was that I was calling the parent method as if it
was a class method

list
Comapny.list_by_service(‘Servicetype’)

Anyways I started reading about the how a ‘type’ column can be used with
child classes but only with has_one, belongs_to relationship.

Is there similar work on objects with HABTM relationships, as in my
example where a company has many services and saving the child model
updates the company details and the service details ?

_Tony