Forum: Ruby on Rails Models and 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.
Anthony G. (Guest)
on 2006-02-27 16:11
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
Anthony G. (Guest)
on 2006-03-02 09:20
> 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
This topic is locked and can not be replied to.