generate 10 UUID records and save it it database in rails

Posted by user662503 on Stack Overflow See other posts from Stack Overflow or by user662503
Published on 2012-10-17T13:21:29Z Indexed on 2012/10/18 5:03 UTC
Read the original article Hit count: 111

Filed under:

I need to create certain number of UUId records (based on the selection of a drop down) and save them 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

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

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 %>

© Stack Overflow or respective owner

Related posts about ruby-on-rails-3.1