How to set up usernames from email of all users?

16 views
Skip to first unread message

regedarek

unread,
Jul 9, 2012, 9:01:47 AM7/9/12
to rubyonra...@googlegroups.com
I would like to create rake task to set the username of all users' without a username to the part before the '@' in their email address. So if my email is te...@email.eu, my username should become test. If it's not available, prepend it by a number (1).

So i have problem witch checking uniqness of username. Code below isn`t working after second loop ex: when i have three emails: te...@smt.com, te...@smt.pl, te...@oo.com username for te...@oo.com will be empty.

I have of course uniqness validation for username in User model.

    desc "Set username of all users wihout a username"
    task set_username_of_all_users: :environment do
      users_without_username = User.select{ |u| !u.username? }
      users_without_username.each do |user|
        username = user.email.split('@').first
        if User.find_by_username(username).blank?
          user.username = username
          user.save
        else
          User.find_by_username(username).each_with_index do |u, index|
            u.username = username.insert(0, index)
            u.save
          end
        end
      end
    end

Tom Meinlschmidt

unread,
Jul 9, 2012, 9:35:32 AM7/9/12
to rubyonra...@googlegroups.com
If you have your validations set, you can use something like

in User model:
scope :without_username, where(:username => nil)
validates :username, :uniqueness => true, :presence => false

and then use
users = User.without_username

users.each do |user|
username = user.email.split(/@/).first
user.username = username
while !user.valid?
user.username+=rand(10).to_s
end
user.save
end

tom
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/4tkg3bBKTbcJ.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.

--
===============================================================================
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
===============================================================================

regedarek

unread,
Jul 9, 2012, 1:50:07 PM7/9/12
to rubyonra...@googlegroups.com
But what if I would like to have usernames with increasing prefix this "test", "1test", "2test"
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.

Tom Meinlschmidt

unread,
Jul 9, 2012, 2:39:39 PM7/9/12
to rubyonra...@googlegroups.com
then try something like (replace while)

users.each do |user|
username = user.email.split(/@/).first
prefix = 1
while !user.valid?
user.username=prefix.to_s+username
prefix+=1
end
user.save
end

tom
> > To post to this group, send email to rubyonra...@googlegroups.com.
> > To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
>
> --
> ===============================================================================
> Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache
>
> www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz
> ===============================================================================
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aKnXNZM24MMJ.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages