How to implement Active Record inheritance in Ruby on Rails?

后端 未结 3 1228
花落未央
花落未央 2020-12-01 02:52

How to implement inheritance with active records?

For example, I want a class Animal, class Dog, and class Cat.

How would the model and the database table ma

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 03:41

    ActiveRecord supports mapping inheritance hierarchies to a single table(Single-table inheritance. Table would have a column type which stores name of actual class and is used to select other class-specific columns.

    It is possible to implement multi-table inheritance mapping, as shown here, but this particular way is not portable, AFAIK.

提交回复
热议问题