Forum: Ruby on Rails Force lowercase username in a model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
45e815356bbcbb7bc165235679cd160d?d=identicon&s=25 Tirta K. Untario (Guest)
on 2007-05-23 16:23
(Received via mailing list)
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.
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-23 16:32
(Received via mailing list)
> 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
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-23 16:33
(Received via mailing list)
> 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
45e815356bbcbb7bc165235679cd160d?d=identicon&s=25 Tirta K. Untario (Guest)
on 2007-05-23 16:45
(Received via mailing list)
This method works:

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

Thanks, Robert! :)
45e815356bbcbb7bc165235679cd160d?d=identicon&s=25 Tirta K. Untario (Guest)
on 2007-05-23 16:46
(Received via mailing list)
This method works:

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

Thanks, Robert! :)
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-05-23 16:49
(Received via mailing list)
Hi --

On Wed, 23 May 2007, Robert Walker 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)
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-05-23 17:00
(Received via mailing list)
> 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 Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-05-23 17:04
(Received via mailing list)
> 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.
5233478c51a92b6a1a5c970cbf3a42f3?d=identicon&s=25 Isak Hansen (Guest)
on 2007-05-23 17:20
(Received via mailing list)
On 5/23/07, Tirta K. Untario <tkuntario@gmail.com> 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
013264c787b7ef3c0f919017d4305b36?d=identicon&s=25 David B. (dendicus)
on 2009-08-30 20:38
Isak Hansen wrote:
> On 5/23/07, Tirta K. Untario <tkuntario@gmail.com> 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.
This topic is locked and can not be replied to.