Help with generating random item number

Hey all,

when a user submits a new item in my app, I want to generate a random
7-digit number for that item. So far, I am a little unclear as to how
this should work.

Here is what I have in my item.rb model

# generate a random item number for each new listing
  def generate_item_number
    rand(9999999).to_s.center(7, rand(9).to_s)
  end

Now, my question is......

Should I create this random number in my items controller right before
it saves the item to the DB?

def create
    # associate user to new item listing
    @item = @current_user.items.build(params[:item])
    -------------GENERATE RANDOM NUMBER HERE--------------???
    if @item.save
    flash[:notice] = 'Your item has been successfully submitted!'
    redirect_to items_path
    else
    flash[:notice] = "Uh Oh! Your item cannot be posted yet."
    render :action => "new"
    end
  end

I am thinking that is best method for doing this, but how would I
actually do it? Is this the cleanest way to produce this number?

Thanks!

Hey all,

when a user submits a new item in my app, I want to generate a random
7-digit number for that item. So far, I am a little unclear as to how
this should work.

Here is what I have in my item.rb model

# generate a random item number for each new listing
def generate_item_number
   rand(9999999).to_s.center(7, rand(9).to_s)
end

I would say 1000000 + rand(8999999) is slightly more obvious.

Now, my question is......

Should I create this random number in my items controller right before
it saves the item to the DB?

def create
   # associate user to new item listing
   @item = @current_user.items.build(params[:item])
   -------------GENERATE RANDOM NUMBER HERE--------------???
   if @item.save
   flash[:notice] = 'Your item has been successfully submitted!'
   redirect_to items_path
   else
   flash[:notice] = "Uh Oh! Your item cannot be posted yet."
   render :action => "new"
   end
end

I am thinking that is best method for doing this, but how would I
actually do it? Is this the cleanest way to produce this number?

I'd do it in a model before_create/before_save. This shouldn't be the
controller's concern

Fred

Thanks Frederick, I see what you area saying now.

So, in my model, I have now added

class Item < ActiveRecord::Base
  #add associations
  belongs_to :user

  before_save :generate_item_number

end

Now, how would I actually have this random number written to my
"item_number" column in the DB? Should I use a hidden field that calls
that method?

--Cory

I concur with Fred. Keep it in the model. If you want to make it more
obvious and/or reusable create a public class method for this and call
that from the before_save/update filters.

As far as true random number generation, that is more of a political
discussion. Using rand should be adequate. I would like to ask, is the
number supposed to be unique across that model, because in theory it
could occur 10X in a row and would not be truly random. If you want
something that is a big number/pkey for that item, i would also look
at using a hash on the item using the model primary key as salt. That
in theory can give you a random looking number/string but in reality
to unique like a pkey too. just a thought.

H

Thanks Frederick, I see what you area saying now.

So, in my model, I have now added

class Item < ActiveRecord::Base
#add associations
belongs_to :user

before_save :generate_item_number

end

Now, how would I actually have this random number written to my
"item_number" column in the DB? Should I use a hidden field that calls
that method?

No. just do all that in the before_save, ie

def my_before_save
  self.item_number = ...
end

(you might want it to be a before_create rather than a before_save)

Fred

Excellent, Thanks Frederick, it works perfectly!

@Blasterpal....

Yes, the number does truly need to be random as well as unique across
the model as I would not want 2 items to ever have the same item
number. Most of these items will (later on) be searched and looked up
by this number, so obviously duplicates can't happen.

So what you are saying is that my rand sequence I posted above could
at some point generate the same number over again?

--Cory

Absolutely (it wouldn't be very random if it never produced values it
has already produced). Any individual number has the same chance of
being produced at any given time. if you want a uniqueness guarantee
you'll have to enforce that yourself (and to be double sure stick a
unique index on the relevant column).

Fred