stig Garet
stig Garet

Reputation: 564

Registration if email already exist with devise

I've a variable registered(boolean) in my table User. Because when a connected user want to use an Email, if this email is not existing into the table User, a new line is created in the Users (only with the variable email, and registered = false). So I would like that when a user registers, he will not be blocked if(email exist && registered == false). In this case, the password will be replaced, and the variable registered change to true.

I would like to run something like that :

def create
    @user_registred = User.find(email: params[user: :email])
    if @user_registred.any?
      if @user_registred.registred == false
        @user = User.update(params[:user])
      else 
        notice: "Email already taked"
      end
    else
      @user = User.new(params[:user])
    end
  end

Upvotes: 0

Views: 940

Answers (1)

Manishh
Manishh

Reputation: 1484

I think devise already provide uniqueness on email. Apart from this instead of having another column you can put validation of uniqueness also here. Apart from this you can also set database level uniqueness constraint.

Update : have a look at the below code.

user = User.where(email: '[email protected]').first_or_initialize
if user.new_record?
  user.save
  Do your stuff.
else
  user.update_attributes(attributes goes here)
  and do your other stuff
end

Upvotes: 1

Related Questions