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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> def username > @username > end > > def username=(user) > @username = user.downcase > enddef username=(user) self.username = user.downcase end or possibly this: def before_save username = username.downcase end On May 23, 10:22 am, "Tirta K. Untario" <tkunta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> def username > @username > end > > def username=(user) > @username = user.downcase > enddef username=(user) self.username = user.downcase end or possibly this: def before_save username = username.downcase end On May 23, 10:22 am, "Tirta K. Untario" <tkunta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 5/23/07, Tirta K. Untario <tkuntario-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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> > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This method works:
def before_save
self.username = self.username.downcase
end
Thanks, Robert! :)
On May 23, 9:32 pm, Robert Walker
<rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
This method works:
def before_save
self.username = self.username.downcase
end
Thanks, Robert! :)
On May 23, 9:32 pm, Robert Walker
<rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2007-May-23 14:48 UTC
Re: Force lowercase username in a model
Hi -- On Wed, 23 May 2007, Robert Walker wrote:> On May 23, 10:22 am, "Tirta K. Untario" <tkunta...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 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 > endThat 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 > endYou''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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> def username=(user) > self.username = user.downcase > endThis 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. On May 23, 10:49 am, "Rick Olson" <technowee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 Olsonhttp://lighthouseapp.comhttp://weblog.techno-weenie.nethttp://mephistoblog.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Isak Hansen wrote:> On 5/23/07, Tirta K. Untario <tkuntario-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > > IsakFor bypassing the infinite loop, you could use this too: def before_save self.username.downcase! end D. -- Posted via http://www.ruby-forum.com/.