0

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
stig Garet
  • 564
  • 2
  • 13

1 Answers1

1

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: 'test@email.com').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
Manishh
  • 1,444
  • 13
  • 23
  • Hi @Manishh, thanks for your answer. Yep, devise provide uniqueness on email, but that's the problem. I would like to check if the User which have email is already registred. And if not, Edit this existing User with the params of the registration. I've put some code that I've tried to give you better explaination. If you have any solution to do that would be great – stig Garet Jan 10 '18 at 11:38
  • So you can search for email instead of having a column. consider that `user = User.where(email: 'test@email.com')` and `if user.present?` do your stuff. else create user with it and do your stuff.. – Manishh Jan 10 '18 at 11:44