For some reasons, I want to restrict the total number of Devise users, the new user won't save when greater than this number.
But I don't know how to do that, I didn't see any content about it in Devise doc.
Could anyone help me? Thanks.
For some reasons, I want to restrict the total number of Devise users, the new user won't save when greater than this number.
But I don't know how to do that, I didn't see any content about it in Devise doc.
Could anyone help me? Thanks.
You could modify the devise registration controller to count the number of users in your db before saving the user or you could add a before create callback in your user model to do the same.
However doing it in the user model will get your user an error that you cannot modify.
If you do it in your custom devise registration controller you could customize the error and how the application handle it.
Implementing this within your model is more appropriate then your controller.
Below is an example of this:
class Moderator < ActiveRecord::Base
before_create :check_moderator_limit
private
devise :database_authenticatable,
:trackable,
:validatable,
:timeoutable,
:registerable
def check_moderator_limit
if Moderator.count > 3
return false
end
true
end
end