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.
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.
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.
class Invoice < ActiveRecord::Base
before_create :setuniqueno
validates_uniqueness_of :uniquenumber
def setuniqueno
self.uniquenumber = Time.now.to_i
end
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
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??
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
@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.
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.
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
So maybe you should be doing that after you create the user.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.