0

I have a model BillService with basic set of fields. Next I want to create two models BillItem and BillGroup, which are the heirs of this and have the additional fields (each has a unique set). I also want to be able to get through the base class all instances of heirs and to check the class of concrete instance.

I can't imagine how to implement this. I tried to google it but nothing succeeded. I read about STI. That's what I need?

1 Answers1

0

ActiveRecord has native support of STI architecture.

  1. Create BillService model

class BillService < ActiveRecord::Base

end

  1. Create an folder bill_service and create your sti model in this folder

class BillService < ActiveRecord::Base

end

  1. Add type field in your table bill_services

add_column :bill_services, :type, :string

  1. Migrate

rake db:migrate

  1. Use your STI

rails c

BillService::BillItem.all

BillService::BillItem.new

it's equal at

BillService.where(type: 'BillService::BillItem')

ActiveRecord set automatically type field by YouModel::YourSubModel.

Gearnode
  • 693
  • 7
  • 21