0

I was wondering how one would create a model which has a foreign key which references the primary key within that same table? For example, consider Tom is the parent of both Beth and Bucky, with a Persons table with the field parent_id:

Persons table
|  id  |   name  | parent_id |
|----  |-------  |---------- |
|  1   |   Tom   |    null   |
|  2   |  Beth   |     1     |
|  3   | Bucky   |     1     |


class Person < ActiveRecord::Base
  belongs_to :person, :foreign_key => 'parent_id'
  has_many :persons, :foreign_key => 'parent_id'
end

Would the above work or is there another way to properly associate this?

user3633260
  • 35
  • 1
  • 3
  • 8

0 Answers0