ruby-on-rails - Ruby on Rails - 从数组创建多条记录

标签 ruby-on-rails arrays database activerecord

我有一个模型的表格 @gig .我正在尝试创建多个 gigs除了 date 之外,立即具有相同的属性应该从数组中选取。

到目前为止,只有数组中的最后一个日期和一条记录被保存。

_form.html.erb

    <%= simple_form_for @gig , url: gigs_path do |form| %>
         <div class="create-title">
            <%= form.input :title, label: t('gig.title'), placeholder: t('placeholder.title') %>
         </div>

          bla... bla... bla....

         <p><%= t('gig.date') %> </p>
           <% if !mobile_device? %>
             <%= form.input :date, as: :string, label: false, placeholder: t('placeholder.date'), multiple: true %>
           <% else %>

                       bla... bla... bla....
    <% end %>

gig_controler.rb

    def create
      @gigdates = params[:gig][:date].split(';')
      puts @gigdates.count
        @gigdates.each do |date|
            puts date
           @gig = Gig.create(gig_params)
           @gig.date = date
         end

        if @gig.save

          redirect_to @gig
        else
          Rails.logger.info(@gig.errors.inspect)
          render 'new'
        end
      end




     def gig_params
       params.require(:gig).permit(:title, :location, :description, :date, :salary, :salary_cents, :salary_currency, :genre_ids => [])
     end

使用 puts date在 Controller 中,我可以看到我的日期被正确分隔。

服务器显示ROLLBACK被调用的次数与数组中的日期一样多,然后是最后的 gig正确保存。


更新 1:

我已经更改了允许创建多个记录的 Controller ,我只是担心如果记录无法保存,则缺少重定向或消息。

def create

  @gigdates = params[:gig][:date].split(';')
    @gigdates.each do |date|
       @gig = Gig.new(gig_params)
       @gig.date = date
       @genres = Genre.where(:id => params[:choose_genres])
       @gig.genres << @genres
       @gig.save
     end

    redirect_to @gig
  end

最佳答案

对于 @gig_dates 数组中的每个日期,您正在使用 gig_params 创建无效的演出。它是无效的,因为 gig_paramsdate 键将是最初在表单中输入的字符串,例如“2017-01-3;2017-05-09;2017-08-01”。这是一个无效日期,因此所有 ROLLBACK - 您对 Gig.create 的调用都在数据库级别失败。

让我们更深入地了解正在发生的事情:

  # EACH LOOP BEGINS
  # for each gig date:
  @gigdates.each do |date|
    # gig_params[:date] will be the whole string that was entered in the
    # form. this is an invalid date, so Gig.create will fail to save the
    # gig to the database (= ROLLBACK). 
    # so you create an gig, but it can't be saved because its date parameter
    # is invalid. you assign this invalid gig to the @gig instance
    # variable. this variable will be overwritten each time, so only the
    # last created gig is stored in @gig
    @gig = Gig.create(gig_params)

    # you assign that invalid gig a (valid) date (but you don't save it!)
    @gig.date = date
  end
  # EACH LOOP ENDS

  # Save @gig, the last created gig, using the valid date you assigned 
  # it at the end of the each loop. so now the save will work!
  if @gig.save
    redirect_to @gig
  else
    Rails.logger.info(@gig.errors.inspect)
    render 'new'
  end
end

编辑:

处理验证的一种方法是在保存任何新演出之前检查所有新演出的有效性。为此,您可以将 each 替换为 map ,这将创建一个新演出数组,然后在保存所有演出之前检查他们是否都有效:

def create
  gigdates = params[:gig][:date].split(';')

  gigs = gigdates.map do |date|
    gig = Gig.new(gig_params.merge(date: date))
    genres = Genre.where(:id => params[:choose_genres])
    gig.genres << genres
  end

  if gigs.all?(&:valid?) && gigs.all?(&:save)
    redirect_to gigs.first
  else
    flash[:notice] = "something went wrong"
    @gig = Gig.new(gig_params)
    render :new
  end
end

关于ruby-on-rails - Ruby on Rails - 从数组创建多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41248213/

相关文章:

javascript - 将输入元素对转换为 json

database - 免费的地理数据(首都、兴趣点……)数据库

mysql - "Invoice"表在专业数据库中是什么样子的?

javascript - 如何对存储在组合框中的数据库结果进行排序

ruby-on-rails - Simple Rails 数组未保存到数组列

java - Java中根据第一个迭代变量对嵌套循环元素进行分组

java - 需要帮助调试: Append two ArrayLists using Method - Java

html - 是否可以在 Netbeans IDE 中自动完成 HTML 标记?

css - 配置 tinymce-rails 以默认使用不同的字体系列

ruby-on-rails - 如何创建复杂的 Nested -> Belongs To 对象?