E. Fernandes E. Fernandes - 1 year ago 54
Ruby Question

Creating tables with almost the same data attributes

So I'm writing a rails app where I need to create two entities, one for suppliers and other for customers. But it happens that they might have almost the same data attributes, let's say for instance name, identity, phone, email and address.
My question is, should I create two different tables to handle that(but it seems kinda redundant to me) or what's the best approach in this case?

Thanks in advance.

Answer Source

I would choose different models, although both share same attributes at this moment, you should separate concerns in a clean way at the model and data level (different tables) because is very likely that you'll find the need for new attributes for each specific model.

There's also the option to separate in a different model just the part of the ContactInfo (phone, email, address) and relate that to each model.

If you choose the path of the same table, you can use STI (Single Table Inheritance) in Rails and separate the models with appropriate methods that correspond to each specific scenario.

In the end I believe you should choose the approach that is more simple and maintainable in the long term.

Check the docs at http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download