views:

21

answers:

1

We have Models like Supplier, Distributor, Vendor, Buyer in our schema. These entities have some common attributes ( like name, description, sales offices etc.) but mostly they have a divergent schema with different has_many :through associations ( a vendor has many stock_keeping_units , others do not) because of which they needed to be collapsed to separate models.

We also have various types of Events in the system, for example, a partnership event, a distributorship event, a procurement event, etc.

How can I create a is_a relationship and abstract these buyer, vendor, etc models to a Company Model and when creating an event , like say a partnership event, simply say Company 1 partnered with Company 2 without worrying what the company type is, so that I can do something like this on my form partial for an Event Submission:

  <p>
    <%= f.label "Company 1" %>
    <%= f.collection_select :partnering_company_id_1, Company.all ,:id, :name, { :default => true} %>
  </p>

I am willing to share more details of the schema in case its needed. Is this possible? I recently came across this blog post which goes ahead and describes MTI but I am not sure if this is applicable to the current problem statement.

A: 

I'd use single table inheritance. I believe this is the best solution when you have models with very similar attributes.

For Events, for example, you'd have only one table (events), which must have a type column.

There's no good documentation about STI (at least I didn't find it), but this post and this one should help a little...

j.
Not too keen on STI since the models only share a few attributes in common and have their own has_many:through associations. Also each of these models would probably be searchable through sphinx individually
papdel
You can have many models. The thing with STI is that you'll have only one table. But if your models doesn't share many attributes, this is not the best solution.
j.