Force lowercase username in a model


#1

Hi guys,

I ran into small problem here, but couldn’t find the solution
anywhere.

I have this model:

create_table :users do |t|
  t.column "username",      :string
  t.column "email",         :string
  t.column "password", :string
end

I want to force User.username to be in lowercase in every save/update.
So, if I fill ‘MeMbeR’, no error raised, but it’s saved as ‘member’ in
the table. I tried this way:

def username
@username
end

def username=(user)
@username = user.downcase
end

But it results in username being NULL in the table.

Any advice would be very helpful. Thanks.


#2

def username
@username
end

def username=(user)
@username = user.downcase
end

def username=(user)
self.username = user.downcase
end

or possibly this:

def before_save
username = username.downcase
end


#3

def username
@username
end

def username=(user)
@username = user.downcase
end

def username=(user)
self.username = user.downcase
end

or possibly this:

def before_save
username = username.downcase
end


#4

This method works:

def before_save
self.username = self.username.downcase
end

Thanks, Robert! :slight_smile:


#5

This method works:

def before_save
self.username = self.username.downcase
end

Thanks, Robert! :slight_smile:


#6

Hi –

On Wed, 23 May 2007, Robert W. wrote:

  t.column "email",         :string

end
That will recurse infinitely (or until you run out of stack space),
because self.username = is a call to the very method you’re defining.

or possibly this:

def before_save
username = username.downcase
end

You’re just assigning to a local variable (username) there.

Try this:

def before_save
self.username = self.username.downcase
end

(You can actually dispense with the second ‘self’ if you wish.)

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)


#7

def username=(user)
self.username = user.downcase
end

This would cause an infinite loop. If you wanted to use a custom
setter you should go with:

def username=(user)
write_attribute :username, user
end


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com


#8

On 5/23/07, Tirta K. Untario removed_email_address@domain.invalid wrote:

  t.column "email",         :string

def username=(user)
@username = user.downcase
end

But it results in username being NULL in the table.

Any advice would be very helpful. Thanks.

AR model attributes aren’t simply instance variables. The api docs for
ActiveRecord::Base explain how you go about overriding the default
accessors.

Isak


#9

This would cause an infinite loop. If you wanted to use a custom
setter you should go with:

hehe, oops yes that true. too quick on the draw there and didn’t
think it through.


#10

Isak H. wrote:

On 5/23/07, Tirta K. Untario removed_email_address@domain.invalid wrote:

  t.column "email",         :string

def username=(user)
@username = user.downcase
end

But it results in username being NULL in the table.

Any advice would be very helpful. Thanks.

AR model attributes aren’t simply instance variables. The api docs for
ActiveRecord::Base explain how you go about overriding the default
accessors.

Isak

For bypassing the infinite loop, you could use this too:

def before_save
self.username.downcase!
end

D.