0

At the moment in my rails app I have a few classes that are different products.

e.g. one example is Circuits.

What I want to do is create a new class named Service and have all the individual product models inherit from it.

Previously my circuit.rb model was

class Circuit < ActiveRecord::Base

but now it is

class Circuit < Service

and I have created a new `Services1 class, simply:

class Service < ActiveRecord::Base
end

In my circuit_controller.rb I have a few functions, the most straightforward being list

def list
  conditions = []
  conditions = ["organisation_id = ?", params[:id]] if params[:id]
  @circuits = Circuit.paginate(:all, :page => params[:page], :conditions => conditions, :per_page => 40)
end

but changing the circuit model to inherit from services results in my circuit list view being empty which I didn't expect.

In my services table I have included a type field for storing which type of product it is but at the moment the table is empty.

Is multi table inheritance the best way to go? The app is quite large so I don't want to have to refactor a lot of code to implement this change.

Single Table inheritance would definitely be a no-go so I am wondering if some kind of association would be better.

update

Just tried following this blog post:

http://rhnh.net/2010/08/15/class-table-inheritance-and-eager-loading

so I have added

belongs_to :service

to my individual product models and then in the services model

SUBCLASSES = [:circuit, :domain]
  SUBCLASSES.each do |class_name|
    has_one class_name
  end
end

then in the service_controller.rb

def list
  @services = Service.all(:include => Service::SUBCLASSES)
end

finally, in the list view I try to inspect and debug the @services variable but it's empty because the query is running on an empty services table, should it not be also running on the circuits and domains tables aswell?

martincarlin87
  • 10,848
  • 24
  • 98
  • 145
  • What is the question? Is it to fix controller action while keeping your current inheritance? If so, what is the reason `#list` failed? I havn't seen any problem there relating to the new inheritance. – Billy Chan Sep 12 '13 at 11:36
  • @BillyChan I'm kind of asking both questions, the best way to associate the new model as the parent of all the existing models and why when I changed the inheritance my controller could no longer find a list of `circuits` – martincarlin87 Sep 12 '13 at 12:34
  • Got it. What is the reason you want to add that intermediate `Service`? Any new functionalities it brought? – Billy Chan Sep 12 '13 at 12:57
  • the main one is so that we can find out what services a customer has, so instead of checking every individual table for an organisation_id we could check the main parent table instead. – martincarlin87 Sep 12 '13 at 13:34
  • I wrote an answer but ended it with lacking of knowing more details. In short, besides inheritance, `include` is another way to go. In Circuit you can include a module Serviceable which let the class acts like a service, then associate it with Organizations. Options are polymorphic association, has_many :through association, and advanced has_many :through with polymorphic association(http://stackoverflow.com/questions/1683265/activerecord-has-many-through-and-polymorphic-associations) – Billy Chan Sep 12 '13 at 15:32
  • thanks, I will take a look, will probably need to upgrade my version of rails though – martincarlin87 Sep 12 '13 at 16:02

1 Answers1

0

My take : I guess even if you are going to use multi table inheritance you will need to (ideally) write a task to put in all the common variables into your Service table. Multi table inheritance doesn't serve its purpose if the common variables are not in the parent table.

Plus it also depends on how many uncommon attributes are there in your existing models. If it is none I would rather use STI.If the structure of the tables are same i would rather use STI.

But i am assuming its is not the same for all the models.

AshwinKumarS
  • 1,303
  • 10
  • 13