0

I want to I achieve the following model structure in my Rails application having 3 separate tables - Value, DecimalValue and TextValue - in which DecimalValue and TextValue inherit from Value. The idea is that Value should contain only the id, and the child tables should refer to that id. How can I implement this based o ROR?

Value model (id, type) DecimalValue (value_id, value:decimal) TextValue (value_id, value:string)

Thanks in advance!

Rui
  • 5,900
  • 10
  • 38
  • 56
  • I think I understand the objective -- you're creating persistent types. This is the kind of thing I would do in Java or C++ because I had to. RoR and ActiveRecord, and for that matter ruby, think about the world very differently. For example, by using a single table with a `string` column you could store values of any of these three types and then figure out what they are in the context of your app. Why would you want to do this in your RoR app -- what's your objective? Provide a little more detail about your goals and you may find that there's a simple way to do what you want. – Tom Harrison Oct 18 '12 at 00:49
  • Hi! Well, the objective is to have class table inheritance, instead of using a single de-normalised table to store the information associated to all child tables. In the future, I may add another child tables (like "SpecialValue" or "ColorValue") and the number of columns in each child may also vary. Anyhow, the objective is to have multi table inheritance. – Rui Oct 18 '12 at 02:07

1 Answers1

0

You should try Polymorphic Associations.

Here is good RailsCast about it