Class Table Inheritance in Rails 3

て烟熏妆下的殇ゞ 提交于 2019-12-03 02:15:59

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.

why not using Single Table Inheritance? for example:

class Person < ActiveRecord::Base
   # some common code here
end

class Driver < Person
   # Driver code
end

class Passenger < Person
   # Passenger code
end

in this way you'll have a common class Person, plus two specific classes derived from it

I'm using the class table inheritance plugin and it working well, http://github.com/brunofrank/class-table-inheritance

I recently forked a promising project to implement multiple table inheritance and class inheritance in Rails. I have spent a few days subjecting it to rapid development, fixes, commenting and documentation and have re-released it as CITIER (Class Inheritance and Table Inheritance Embeddings for Rails).

I think you could potentially combine it with the answers above?

Consider giving it a look: http://peterhamilton.github.com/citier

I looked at solutions like the class-table-inheritance repo, but this is much cleaner and more logical IMO.

It also supports the main SQL languages SQLite, MySQL, PostgreSQL and probably more but I haven;t tested them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!