Denis Davydov
Denis Davydov

Reputation: 462

Ruby on Rails, find if a certain value exists in a column

I'm building a website with user authentication. And I just noticed that if I create a user with an existing email, it just doesn't work which is normal, but I'd like to give feedback to the user. So for that I need to determine if any user has already that email. I've tried some things like:

if User.email.include? params[:user][:email]
    flash.now[:error] = "A user with this password already exists"
    render :action => :new, :layout => 'signin-layout.html.erb'

Those are the columns for User:

2.1.0 :014 > User.column_names
=> ["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_token", "admin", "team_id", "teamLeader"]

And the result I get is a big fat error:

undefined method `email' for #<Class:0x00000102b9a908>

So if anybody sees what I'm doing wrong, or knows another way to do it, that would be great.

Cheers

Upvotes: 0

Views: 1738

Answers (2)

Thresh
Thresh

Reputation: 470

I believe this way of doing the validation is wrong, you should validate the uniqueness of the email in the User model itself like below

validates :email, uniqueness: true #User model

This way the validation would be on the the User model. The problem with the condition you are using is that it is accessing an instance method specific to objects as a class method. So User.email means that there is a method called email that has the same logic for all the instances of the user class or more formally a class method which you don't have here. The email is an attribute specific to each user an instance attribute/variable (Each user has a different email).

You can see/show the validation errors present on the model using @user.errors.full_messages where @user is the instance you are trying to register/save.

This is how I would normally do it if this action is for registering users i.e. creating new users.

class User < ActiveRecord::Base
    #attribute accessors and accessible
    validates :email, uniqueness: true     
end
class UsersController < ApplicationController
    def create
        @user = User.new params[:user]
        if @user.save
            #code for redirect or rendering the page you want
        else
            render 'new'
        end
    end
end
#new.html.erb
<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div>
      <ul>
         <% @job.errors.full_messages.each do |msg| %>
           <li><%= msg %></li>
         <% end %>
      </ul>
    </div>
  <% end %>
  #form fields
<% end %>

This way you display all the error messages to the user at the top of the registration form.

Upvotes: 2

Surya
Surya

Reputation: 16012

Try this:

if User.exists?(:email => params[:user][:email])
    flash.now[:error] = "A user with this password already exists"
    render :action => :new, :layout => 'signin-layout.html.erb'
    ...
else 
  # more code here..
end

Also, you can add validations when you're creating the object:

class User
  validates_uniqueness_of :email

More on different validations here: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

Upvotes: 6

Related Questions