Modeling data that is stored the same in the db but different in the code

Hey guys,

I’m modelling a data in an app. Think of a document, this document has a set of parts part a. part b. part c. part d.

Each of these has the same data model, i.e. each part has title, description, and one or more images. The title for each is simple which part it is - part a has title “Part A”.

The document needs part A, zero or more part Bs, zero or more part Cs, and part D.

I’m thinking of modelling the document as having 4 parts, one for each part listed above.

The underlying table representation for a Part would have a title, is_enabled, and an array of data where data is subtitle, description, and optional images. I’m trying to avoid a table with the crappy name part_data but maybe that’s better than an array.

Then in the actual code I can have a separate module for each Part A/B/C/D which rely on the same table but represent it differently… I don’t know exactly how to accomplish this in code. I’d guess each would be part of a document context. I’d like to treat PartA as a module and PartB as a module, etc. separately.

Does that sound like both a valid way and the most sound way to model this? I’ll be generating the document based off of these parts (the ones that were enabled).

Probably you could simply use different schema's for it? So each schema would be named whatever you want, reference the same table “Part”, but then be custom, and in your “Document” you would have, has_one :part_type_a, has_many :part_type_b…?

1 Like