Force lowercase username in a model

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.

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

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

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.

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

Isak

This method works:

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

Thanks, Robert! :slight_smile:

This method works:

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

Thanks, Robert! :slight_smile:

Hi --

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.

--~--~---------~--~----~------------~-------~--~----~

def username   @username end

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

def username=(user) self.username = user.downcase 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

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

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.