Nishant Arora
Nishant Arora

Reputation: 388

Allow a user to create another user which belongs to him/her?

I have an Institute model using devise. I want to allow an Institute to create another devise model, Users. I want the created user to belong to the institute that created it by adding an institute ID to the User model.

But, for that I think I'll have to override the default registrations controller for just the User model. If I do that, that'll override it for the Institutes model too (not sure about this), which I don't want.

To be clear, I don't want to use roles in just one user model. I want the institute model to be separate. My user model will have separate roles (a teacher and a student), which the institute can set while creating.

How do I do it?

Upvotes: 0

Views: 69

Answers (1)

bosskovic
bosskovic

Reputation: 2054

How about something like this:

class User < ActiveRecord::Base
  belongs_to :institute
end

class Institute < ActiveRecord::Base
  has_many :users
end

In your user creation form, you add a hidden field institute_id that corresponds to the currently signed in institute.

If you also want to allow the users to register, then you add a select whose keys are institute ids and values institute names.

You can also reuse the same form partial by conditionally rendering the hidden field or the select.

Upvotes: 0

Related Questions