Monday, August 15, 2011

[Rails] Re: newbie: before_create :setuniqueno

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@googlegroups.com.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate