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 ###### 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. any pointers? thx -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
hi philip so i should use a loop while !@invoice.valid? in the task create action(until its valid?)? thx On Mon, Aug 15, 2011 at 4:29 PM, Philip Hallstrom <philip-LSG90OXdqQE@public.gmane.org> wrote:>> 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 > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Aug 15, 2011 at 12:57 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 TimestampIt''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?? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. On Mon, Aug 15, 2011 at 4:49 PM, Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Aug 15, 2011 at 12:57 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> 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?? > > -- > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassan > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Aug 15, 2011 at 1:52 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.Still not sure why, but OK -- google `ruby UUID` :-) - Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Mon, Aug 15, 2011 at 4:59 PM, Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Aug 15, 2011 at 1:52 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 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. > > Still not sure why, but OK -- google `ruby UUID` :-) > > - > Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassan > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Aug 15, 2011 at 2:04 PM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> @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 wayYou 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 :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.