Class Table Inheritance in Rails 3

后端 未结 4 408
无人共我
无人共我 2021-02-05 16:04

I\'m currently working on a Rails 3 application that looks like it might need to use Class Table Inheritance for a couple of models.

A simplified example of what\'s goin

4条回答
  •  太阳男子
    2021-02-05 16:38

    Also there is a plugin 'acts_as_relation' to do this,
    https://github.com/hzamani/acts_as_relation/

    in your case the code will be this:

    class Driver < ActiveRecord::Base
       acts_as :person
    end
    
    class Passenger < ActiveRecord::Base
      acts_as :person
    end
    

    Don't forget to add person_type and person_id columns to persons table.
    Now both Drive and Passenger inherit Person attributes, validations and methods.

提交回复
热议问题