class Member < ActiveRecord::Base end class Demander < Member end class Supplier < Member end
如何在实现 Demander 特有的属性只在 Demander 中使用,Supplier 特有的属性只在 Supplier 中生效,公共的属性存在 Member 中
has_one
关键字Single-table inheritance
Single-table inheritance
ruby 能否实现多表继承
http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html
表结构:
class User < ActiveRecord::Base end class Group < User end group = Group.create group.type => "Group" user = User.create user.type => "User"
多态的话,楼主是这个意思吗?
class Member < ActiveRecord::Base # resource_id, int , 'demanders 或是 suppliers 的id' # resource_type, string, 'Demander 或是 Supplier' has_one :resource, :polymorphic => true end class Demander < Member belongs_to :members, :as => :resource end class Supplier < Member belongs_to :members, :as => :resource end
我觉得单表继承也能实现你的需求