check for unique username instead of email on creation

1,674 views
Skip to first unread message

Nehal Soni

unread,
Dec 11, 2010, 10:33:52 AM12/11/10
to Devise
Hi,

I'm using Rails 2.3.8.

I have a user setup on devise, on signup they provide a username,
email and password and subdomain (i.e company name).
I wish to create many users for a given company, as long as the
username is unique. So multiple emails should be allowed.
When I try the above I get an "email is already taken" validation
error message. I removed the validatable module but an SQL Exception
is caught indicating that the email is not unique.

My quuestion is, can remove the email uniqueness validation, and just
ensure the company and username is unique for a given user.


class User < ActiveRecord::Base

belongs_to :company

devise :database_authenticatable, :recoverable,:rememberable, :trackable, :validatable, :authentication_keys
=> [:username, :company_id]

accepts_nested_attributes_for :company

validates_uniqueness_of :username
validates_presence_of :username

validates_associated :company

validates_format_of :username, :with => /^\w+$/i,
:message => "can only contain letters and numbers."

def self.find_for_authentication(conditions={})
conditions[:companies] = { :name =>
conditions.delete(:company_id) }
find(:first, :conditions => conditions, :include => :company)
end

end

Carlos Antonio da Silva

unread,
Dec 11, 2010, 10:39:10 AM12/11/10
to plataforma...@googlegroups.com
Just remove the validatable module and add the other required validations (such as the password and confirmation) by yourself.
You can copy base validations from Devise validatable module and remove the email ones.
--
At.
Carlos A. da Silva

Nehal Soni

unread,
Dec 11, 2010, 10:42:47 AM12/11/10
to Devise
apologies, I searched the forum again and there was a previous post
regarding this...

http://groups.google.com/group/plataformatec-devise/browse_thread/thread/43370205fee5fbe8/7ddc14ea09117663?lnk=gst&q=unique+email#7ddc14ea09117663

thanks Carlos

On Dec 11, 3:39 pm, Carlos Antonio da Silva
Reply all
Reply to author
Forward
0 new messages