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 service
s 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?