Devise with parent user and child user

Asked

Viewed 96 times

1

I’ve got it in my Class User in the Vise

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

What I’d like to know is what the migration of User in a case like this?

  • 1

    Márcio, off the record, can I give you a hint? Don’t use the Devise, it’s kind of illusory - it’s not so miraculous and you’ll have these little setbacks because of its particularities. If you want to understand a little better, in my blog I created a post just about that.

  • 1

    @Guilhermeoderdenge I found your post very interesting. I’ve made an application with authentication from Scratch and I’m testing the Devise on one that I’m currently developing. My question here is whether it is worth "uninstalling" the Devise now or if I leave anyway. =)

  • 1

    Hello, @user3153542 (it’s time to change nick, ein? hahaha). Boy, in my opinion, if you’re ready and you’re not bothering yourself, so keep it that way. if you’re in some trouble, then I’ll tell you a story... I’ve used Devise in an application of mine and it was there, stable and operandis. One day, I had to inject some complexity and it just gave me problems. I thought like this: You know what? What the hell! I rubbed a rubber on what I had done so far.

  • 1

    @Guilhermeoderdenge I switched the nick =). As for the application, I will keep the same Devise.

  • Now yes, @Andrey!

  • Interestingyour post @Guilhermeoderdenge. As my application will have a certain complexity, I will follow your tip and create auth from scratch and improve as demand and security ask, thank you so much for the interaction! :)

Show 1 more comment

1 answer

2

I haven’t tested it in practice, but try the following.

Once created the model via 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.