ruby rails
2012-Oct-17 13:34 UTC
generate 10 UUID records and save it it database in rails
I need to create certain number of UUId records(based on the selection
of drop down) and save it in the database. Now I am generating only one
unique id. Can this be done in the model in this way. Or do I need to
write a helper file for that??
def generate_unique_token=(value) self.secret
Base64.encode64(UUIDTools::UUID.random_create)[0..8] end
In my controller...........
def create
@secretcode= Secretcode.new(params[:secretcode])
@user= User.new(params[:user])
@secretcode.user_id=@user
@secretcode.generate_unique_token=params[:secretcode][:secret]
if @secretcode.valid?
@secretcode.save
redirect_to secretcodes_path
else
render ''new''
end end
In my view page
> <%=form_for(@secretcode) do |f|%> <%= f.select(:secret,
> options_for_select([[''1'', 1], [''10'',
10], [''20'',
> 20],[''50'',50],[''100'',100]])) %>
<%= render ''layouts/error'' %>
> <%=f.label :secret%> <%= f.hidden_field :user %> <%=f.submit
> :generate%> <%end%>
--
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 https://groups.google.com/groups/opt_out.
Jordon Bedwell
2012-Oct-17 13:38 UTC
Re: generate 10 UUID records and save it it database in rails
On Wed, Oct 17, 2012 at 8:34 AM, ruby rails <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I need to create certain number of UUId records(based on the selection > of drop down) and save it in the database. Now I am generating only one > unique id. Can this be done in the model in this way. Or do I need to > write a helper file for that?? > > def generate_unique_token=(value) self.secret > Base64.encode64(UUIDTools::UUID.random_create)[0..8] end > > In my controller........... > > def create > @secretcode= Secretcode.new(params[:secretcode]) > @user= User.new(params[:user]) > @secretcode.user_id=@user > @secretcode.generate_unique_token=params[:secretcode][:secret] > if @secretcode.valid? > @secretcode.save > redirect_to secretcodes_path > else > render ''new'' > end endbefore_save :guarantee_uuid! private def uuid if !uuid_field? write_attribute :uuid_field, # UUID CODE HERE end end That is how I would personally do it, put it all in a method and trigger it with before_save, this also gives you the option to generate the UUID and modify/add it in other places too. -- 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 https://groups.google.com/groups/opt_out.