Rails The different types of models have a different number and type of fields

advertisements

In rails web app, user can "make" a Document. There are different types of documents :

  1. Loan
  2. Business
  3. Insurance

Each type of Document will have some things in common such as: account_num, doc_id, at least 1 name, but then they have different attributes.

For example:

Loan is only doc with a loan_type field

Business documents can have 1+ name attributes

If these docs may have different number of attributes, do they need to be completely separate models, or is there a way to incorporate a doc_type attribute for Document which then reveals what, and how many, attributes are associated with the Document? If so, what would that look like?


What you're describing is the express purpose of single-table inheritance in Rails.

Use one table with a super-set of all the fields from all the models. Add a type column, and then create your three models, inheriting from a base model, and you're pretty much done.