Use of STI(Single Table Inheritance) on Rails

Asked

Viewed 122 times

1

  • Assuming a project in Rails with the models Livestock, Agriculture, Piscicultura e Benfeitoria.
  • These inherit the properties of a model called Activity and are considered types of activity.
  • Models do not accurately inherit specific methods or behaviors, in the case of access only to the inherited properties
  • But some models have properties beyond the inherited and others no, for example Agriculture , beyond the inherited, has the Grupoagricola properties and Tipodeagriculture.

Bearing this in mind comes my doubts: Is it a favorable scenario for the use of the STI (Single Table Inheritance) technique? 2nd In the case of models that have only inherited properties, there is how to avoid creating a new table or persistence model?.

  • 1

    Have you tried modeling with composition instead of Heritage?

1 answer

2


  • but the fact that I want to prevent some kinds of activities from persisting, how should I do? , just do not create the corresponding Model but include it in the type list, for example 'def Activity.types %w{Type1 Type2 Type3}' but do not create the type3.Rb file?

  • I did not understand, because they would not be persisted?

  • Because for what I understood using STI all properties inherited from 'Base' are saved in the table referring to base ex: the model X has the properties X1, x2 and X3 the models XA and XB extend X but the model XA also has the properties xa1, xa2 and xa3, and XB have none beyond. However, 3 tables are created in table x with the fields (id, X1, x2 , X3, created_at and modified_at) the table Xa with the fields (id, xa1, xa2 , xa3, reated_at and modified_at) and the table xb s with the fields (id, reated_at and modified_at). But I do not see the need to create xb

  • 1

    No, tables are not created for XA or XB, all models use the same table.

  • 1

    Read the links I passed in my reply, there teaches to record the "extra" fields in a json field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.