单表继承STI实现一对多关联

代码如下:

create_table :organizations do |t|
  t.string    :type, :name
  t.integer   :priority, :organization_id
  t.timestamps
end



class Organization < ActiveRecord::Base
end

class Headquarter < Organization
  has_many :filiales, :foreign_key => :organization_id
end

class Filiale < Organization
  belongs_to :headquarter, :foreign_key => :organization_id
end


这样同个表中继承出来两个类也可以实现一对多的关系,和两个表的做法完全相同。

你可能感兴趣的:(Ruby,ActiveRecord)