newbie: before_create :setuniqueno

hi,

i was wondering how this should be handled.imagine i have this class:

class Invoice < ActiveRecord::Base
before_create :setuniqueno
validates_uniqueness_of :uniquenumber
def setuniqueno
  self.uniquenumber = Time.now.to_i
end
end

i was wondering how this should be handled.imagine i have this class:

class Invoice < ActiveRecord::Base
before_create :setuniqueno
validates_uniqueness_of :uniquenumber
def setuniqueno
  self.uniquenumber = Time.now.to_i
end
end
######
class Task<ActiveRecord::Base
end
######
after the user post to the create function i also create an Invoice.
what is not 100% clear to me, is what happens/how should i handle the
case if the uniquenumber is not unique? eg if multiple user generate
the same Timestamp
how does the task-controller get feedback from the Invoice-Model?
im doing a find_or_create_by in the create action of the task.

In your controller, assuming you have an @invoice variable you can call @invoice.valid? on it to see if it was found/created successfully.

-philip

hi philip

so i should use a loop while !@invoice.valid? in the task create
action(until its valid?)?
thx

It's not clear what you're trying to do here. Each invoice will have a
unique ID by default; what's this additional "unique number" for??

in terms of an invoice i would eg like to display a uniqe number. yes
, i could use auto-inc, which is the internal id, but sometimes u just
need a ssecond one.
so i was wondering how i would generate that during eg a create action.

Still not sure why, but OK -- google `ruby UUID` :slight_smile:

i know uuid etc.
@why
sometimes u just need to display another e 10digits number which u can
trace back and is still unique
@how
still not sure if a loop is the right way

You can just try to save and be prepared to catch the validation
failure, which will require you to loop until the save succeeds.

Or you could pre-generate a list of guaranteed unique invoice ids to
use, which would eliminate all that.

TMTOWTDI :slight_smile:

Tom Tom wrote in post #1016803:

in terms of an invoice i would eg like to display a uniqe number. yes
, i could use auto-inc, which is the internal id, but sometimes u just
need a ssecond one.
so i was wondering how i would generate that during eg a create action.

If you use the internal id, which is unique, and add a string to it, it
is guaranteed to be unique:

ids = [1, 2]

ids.each do |id|
  str = 9.times.map {rand 10}.join
  puts "#{id}-#{str}"
end

--output:--
1-421152858
2-513987885

in terms of an invoice i would eg like to display a uniqe number. yes
, i could use auto-inc, which is the internal id, but sometimes u just
need a ssecond one.
so i was wondering how i would generate that during eg a create action.

Unless you are trying to store some meta information in that unique number, I'd suggest against it. If you don't like invoices going out with #1, #2, #3, etc, then just add 100,000 to the id and there's your invoice number.. 100001, 100002, etc...

-p