开发者

Polymorphic abstraction of Models and potential MI in rails?

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.


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...


I don't know if this is still relevant but I recently worked on a MTI solution. It is mega simple and works great, seems like exactly what you need(ed) :)

Consider giving it a look: http://peterhamilton.github.com/citier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜