0

I am looking for a method that can generate a random string in the starting of the email field while updating the record.

def update
  @user = User.find_by_id(4)
  @user.email = #method to update email with random string
end

So if I have the email record abc@gmail.com and I want to update it like this:

dssaakj123_abc@gmail.com

How it can be done in rails?

davegson
  • 8,205
  • 4
  • 51
  • 71
user4965201
  • 973
  • 1
  • 11
  • 25
  • 1
    Have you check this [Question: How best to generate a random string in Ruby](http://stackoverflow.com/questions/88311/how-best-to-generate-a-random-string-in-ruby)? – Gagan Gami Dec 02 '15 at 10:34
  • just update the setter – apneadiving Dec 02 '15 at 10:38
  • 1
    Do you intend to prepend the email with the random string *once* or on *every* update? – davegson Dec 02 '15 at 10:44
  • 1
    I'll just warn you that the current solutions will all lead to emails updated on every update call. Resulting in `foo1_foo2_foo3_.._foo100_champ@gmail.com`. I this really what you want? – davegson Dec 02 '15 at 11:14

5 Answers5

4

You can use the SecureRandom library:

@user.email = "#{SecureRandom.hex(10)}_#{user.email}"
Yury Lebedev
  • 3,985
  • 19
  • 26
  • how can i get a random string only in digits ? – user4965201 Dec 02 '15 at 10:51
  • Use `'%011d' % rand(1e10)` instead of `SecureRandom.hex(10)`. To change the number of digits in the string change the number after `e` here: `1e10` – Yury Lebedev Dec 02 '15 at 10:53
  • SecureRandom is part of [the standard lib](http://ruby-doc.org/stdlib-2.2.0/libdoc/securerandom/rdoc/SecureRandom.html), no ActiveSupport needed. – steenslag Dec 02 '15 at 12:37
2

Why not use SecureRandom?

require 'securerandom'
random_string = SecureRandom.hex # provide argument to limit the no. of characters

# outputs: 5b5cd0da3121fc53b4bc84d0c8af2e81 (i.e. 32 chars of 0..9, a..f)

For appending before email, you can do something like

@user.email = "#{SecureRandom.hex(5))_#{@user.email}" # 5 is no. of characters

Hope it helps!

Dusht
  • 4,712
  • 3
  • 18
  • 24
  • just a small thing: i think the author if the question wanted to prepend a random string to the email address, not replace the whole string before `@` completely – Yury Lebedev Dec 02 '15 at 10:33
  • @YuryLebedev Yes it might be the case. But he didn't mentioned. It might be the case, he wants to replace before `@`. Author needs to confirm on this.:( – Dusht Dec 02 '15 at 10:35
  • Yes, you are right. He mentioned at the starting, that he wants to `generate a random string in the starting of the email`. – Dusht Dec 02 '15 at 10:38
1
(1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join

8 is the number of characters you want to generate randomly.

Vrushali Pawar
  • 3,753
  • 1
  • 13
  • 22
-1

create an action in your application controller like this :

private 
  def generate_random_string
    SecureRandom.urlsafe_base64(nil, false)
  end

And use it like this in any controller you want:

def update
  @user = User.find_by_id(4)
  @user.email = generate_random_string + @user.email
end
Dipak Gupta
  • 7,321
  • 1
  • 20
  • 32
-1

I hope this will help you.

def update
  @user = User.find_by_id(4)
  @user.email = "#{generate_random_string(8)}_#{@user.email}"
  ## You can pass any length to generate_random_string method, in this I have passed 8.
end

private

def generate_random_string(length)
  options = { :length => length.to_i, :chars => ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a }
  Array.new(options[:length]) { options[:chars].to_a[rand(options[:chars].to_a.size)] }.join
end
Amit Sharma
  • 3,427
  • 2
  • 15
  • 20