I am creating a simple user registration functionality and making user input firstname, lastname and email. Email field should be unique for all users. I have given this constraint in db. In code though, when a row with duplicate email is input, it does not create a row in db but does not raise any exception too. I am stuck on how to show error to user.
My code:
table creation script:
CREATE TABLE customers (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), firstname VARCHAR(25), lastname VARCHAR(25), email VARCHAR(50), UNIQUE KEY(email), password VARCHAR(50));
def self.create_new(params)
begin
customer = Customer.create!(:firstname=>params[:firstname], :lastname => params[:lastname], :email => params[:email], :password => params[:password])
return {:success => true, :message => "success", :id => customer.id }
rescue => e
return {:success => false, :message => e.message}
end
end
Should I add a uniqueness validation at model level to implement this?