Posts [ 1 ]

Topic: How to implement a find_or_create_user if User model uses Devise?

Hi, I'm building a Rails 3.2 app, with two user models - Admin and User - where Admins can create Users using the standard CRUD interface. This works fine, but password and email have to be set in new Users, due to Devise validations.

On a Booking model, Users can be associated using a find_or_create text field on their full name. Currently this works to associate existing users, but is not creating new users. I assume this is because Devise's validation of password and email is failing.

Password is fairly easy to resolve, by automatically setting a password and notifying the user using mailer.

The email issue is more complex. Has anyone else run into an issue like this? What would be a sensible way to solve this, while maintaining the required workflow?

I appreciate that this is a very open ended question, but what I'm really looking for is a sensible and Rails-way approach.

The key considerations are:

1. This is occurring within a nested form, therefore multiple users (mixture of new and existing) can be assigned and created at once.2. When the form is saved (or before) I need to detect which users are new, and allow the Admin to set email values for these users, so that the User create method will validate.