Devise with parent user and child user

1

I have this in my Class User in devise

class User
     belongs_to :parent, :class_name => 'User'
     has_many :children, :class_name => 'User'
     ...
    end

What I would like to know is what would be migration of User in a case like this?

    
asked by anonymous 06.05.2014 / 17:12

1 answer

2

I did not test in practice, but try the following.

Once the model is created via the Devise generator, create a migration with the following:

def change
  change_table(:users) do |t|
    t.references :parent, index: true
  end
end

Then do:

class User < ActiveRecord::Base
  has_many :children, class_name: 'User',
                      foreign_key: 'parent_id'

  belongs_to :parent, class_name: 'User'
end

Be sure to check this section of the official guide .

    
06.05.2014 / 17:58