ruby-on-rails - 希望能够根据两个不同的值进行多行插入

标签 ruby-on-rails ruby ruby-on-rails-4 rails-activerecord

是否可以根据两个不同的列值为一条记录创建多行?

示例:在我的模型条目表中,我有一个名为 full_range 的列,这将使日期在 leave_startleave_end 之间选择日期,例如,假设我选择 05/01/15 作为我的 leave_start 日期和 05/05/15 作为我的 leave_end 日期,这会给我 05/01/15, 05/02/15, 05/03/15, 05/04/15, 05/05/15 我的 全范围

然后对于我的另一列名为 range_days 的值将为 5 这是因为我在 05/01 之间有 5 天/1505/05/15

我想做的是根据 range_days 拆分我的 full_range 值,我想为我的完整范围内的每个日期插入多行,我猜 range_days 会发挥作用说创建要创建的行的值。

现在我只得到一行......

ID     Created_at Full_range_date   emp_id    range_days  leave_start leave_end   full_range     
10686   1-May-15    5/1/2015         TEST1        5        05/01/15   05/05/15     05/01/15 05/02/15 05/03/15 05/04/15 05/05/15     

所以从理论上讲,我希望在我的数据库中看到的是这样的,所以它首先查看 full_range 并获取第一个日期,将其填充为 full_range_date 然后查看在下一个和下一个...基于 range_days 它执行 5 天,即 5 行。

ID     Created_at Full_range_date   emp_id    range_days  leave_start leave_end   full_range     
10686   1-May-15    5/1/2015         TEST1        5        05/01/15   05/05/15     05/01/15 05/02/15 05/03/15 05/04/15 05/05/15     
10687   1-May-15    5/2/2015         TEST1        5        05/01/15   05/05/15     05/01/15 05/02/15 05/03/15 05/04/15 05/05/15     
10688   1-May-15    5/3/2015         TEST1        5        05/01/15   05/05/15     05/01/15 05/02/15 05/03/15 05/04/15 05/05/15     
10689   1-May-15    5/4/2015         TEST1        5        05/01/15   05/05/15     05/01/15 05/02/15 05/03/15 05/04/15 05/05/15     
10690   1-May-15    5/5/2015         TEST1        5        05/01/15   05/05/15     05/01/15 05/02/15 05/03/15 05/04/15 05/05/15     

我该怎么做才能做到这一点,我们将不胜感激!!!

我正在使用 rails 4.1.8

还有额外的信息,这里是我的入口 Controller 。

class EntryController < ApplicationController



  def new
    @entry = Entry.new

    respond_to do |format|

      format.html# new.html.haml
      format.xml { render :xml => @entry }
   end
 end


  def create
    params.permit!
    @entry = Entry.new(params[:entry])
    @entry.t_d
    @entry.day_hours
    @entry.current_user = current_user

    respond_to do |format|

      if @entry.save
        if current_user.email.nil?
          @entry.create_totals
          format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created, but you will not recieve any notifications, because you email is blank!') }
          format.xml { render :xml => @entry, :status => :created, :location => @entry }
        else    
          @entry.create_totals
          EntryMailer.submit_for_approval(@entry).deliver
          format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created.') }
          format.xml { render :xml => @entry, :status => :created, :location => @entry }
        end 
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
      end
    end
  end

和我的入门模型

class Entry < ActiveRecord::Base

  self.primary_key = 'id' 
end 

根据给出的答案,我尝试了这个,但它仍然只创建了一行我想做的是为 full_range 中的每个日期创建一个新行

 def create
   params.permit!
   @entry = Entry.new(params[:entry])
   @entry.t_d
   @entry.day_hours
   @entry.current_user = current_user

   # send my email
   respond_to do |format|

     begin
       Entry.transaction do
         @entry.full_range.split(' ').each do |date|
           entry = Entry.new( @entry.attributes.to_options )
           entry.full_range = date
           entry.save!
         end
       end

       if current_user.email.nil?
         @entry.create_totals
         format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created, but you will not recieve any notifications, because you email is blank!') }
         format.xml { render :xml => @entry, :status => :created, :location => @entry }
       else
         @entry.create_totals
         EntryMailer.submit_for_approval(@entry).deliver
         format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created.') }
         format.xml { render :xml => @entry, :status => :created, :location => @entry }
       end

     rescue
       format.html { render :action => "new" }
       format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
     end
   end
 end

只是为了笑,我试着像在我的入门模型中那样做一个原始的 sql 语句

就像这样插入了正确数量的行,但所有数据都完全匹配,没有任何变化。

  def trying_it_all
    @id = self.id
    @leave_end = self.leave_end.to_date
    @leave_start = self.leave_start.to_date
    @range_vals = self.range_days
    if !(self.range_days == 0)  
       range_days.times do 
      sql = "insert into entry values('#{@id}', '#{@c_d}', '#{@c_u}', '#{@emp_id}', '#{@range_vals}', 'N')"
      Entry.connection.execute(sql)
    end
  end

最佳答案

这是绝对可能的,在您的实现过程中需要考虑很多事情。首当其冲的将是您的#create 操作。您已经拥有所需的所有信息,所以这就是我个人的处理方式。

首先,一次创建多条记录时,always use a transaction .如果您不熟悉,这个想法是只有在每条记录都成功保存时才会对数据库进行更改(因此,如果其中一条记录失败,它将回滚所有其他记录,以防止您的数据变得不一致)。一个实现看起来像这样:

  def create
    params.permit!
    @entry = Entry.new(params[:entry])
    @entry.t_d
    @entry.day_hours
    @entry.current_user = current_user

    respond_to do |format|

      if # Condition here

        ActiveRecord::Base.transaction do
          # Create your records here
        end

      else
        # Indicate failure
      end

    end
  end

现在,您可能会发现没有为 if 语句列出条件。这是因为对于多个事务,我们需要一种更好的方式来判断它们是否成功,而不仅仅是 @entry.save 的结果。最简单的方法是 with a Begin/Rescue block (您可能认为这是来自更多主流语言的 Try/Catch)。它看起来像这样:

respond_to do |format|

  begin       
    ActiveRecord::Base.transaction do
      # Create your records here
    end
  rescue
    # Indicate failure
  end

end

其工作方式是开始 block 将执行,交易将开始。在事务内部,如果出现问题,我们将引发错误。整个事务将回滚,错误将使我们跳出 Begin block ,转而进入 Rescue block 。

现在,在交易中,我们需要创建多条记录。这部分应该相当简单。我们需要使用一个循环来根据 range_days 创建一些记录。在交易中,我们想要做这样的事情:

(1..@entry.range_days).each do
  entry = Entry.new( @entry.attributes.to_options )
  entry.full_range_date = # Calculation to determine the date of this entry
  entry.save!
end

这将为 each day in range_days 创建一个条目.循环中的第一行创建一个与@entry 具有相同值的非实例变量。第二行是您将更改 full_range_date 的值的地方。第三行使用.save!功能,which has an important difference compared to the .save function ;如果失败,它将引发错误。这是一个触发器,如果​​出现任何可怕的错误,您可以逃离开始 block 并跳转到救援 block 。

关于设置新的 full_range_date 的计算,这将涉及日期函数或字符串操作(取决于您处理日期的方式)。 请参阅我在答案底部的 REVISION,了解如何完成此操作。因此,从本质上讲,您的创建函数可能看起来很像这样:

def create
    params.permit!
    @entry = Entry.new(params[:entry])
    @entry.t_d
    @entry.day_hours
    @entry.current_user = current_user

    respond_to do |format|

      begin       
        ActiveRecord::Base.transaction do
          (1..@entry.range_days).each do
            entry = Entry.new( @entry.attributes.to_options )
            entry.full_range_date = # Calculation to determine the date of this entry
            entry.save!
          end
        end

        if current_user.email.nil?
          @entry.create_totals
          format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created, but you will not recieve any notifications, because you email is blank!') }
          format.xml { render :xml => @entry, :status => :created, :location => @entry }
        else    
          @entry.create_totals
          EntryMailer.submit_for_approval(@entry).deliver
          format.html { redirect_to(entry_path( @entry ), :notice => 'Entry successfully created.') }
          format.xml { render :xml => @entry, :status => :created, :location => @entry }
        end 

      rescue
        format.html { render :action => "new" }
        format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
      end

    end
end

结束语

params.permit!是一种令人难以置信的诱人方法,因为它使您不必担心强大的参数,或者在模型更改时无需更新 Controller ……但它非常危险。用户不仅可能向您传递您不希望的字段(从而迫使您处理您没有准备好的大量数据),它还允许隐藏字段由精明的用户定义。例如,如果我向您的条目发送了一个 POST 请求,我可以指定 { :Created_At => 1-Jan-2015 },让它看起来像是我在四个月前创建了这条记录,并且完全贬值了该列。在这种情况下,这并不重要,但假设您有一个带有 :is_administrator 字段的用户模型。然后任何人都可以创建具有管理权限的用户。 This may be worth reading ,如果您有兴趣的话。

修订

快速补充!如果您的 full_range 变量中已有 Full_range_date 值,则可以用以下代码替换原始循环:

@entry.full_range.split(' ').each do |date|
  entry = Entry.new( @entry.attributes.to_options )
  entry.full_range_date = date
  entry.save!
end

这会将 full_range 值变成一个数组,遍历每个元素,并为您设置 full_range_date 的值,无需进一步计算。

关于ruby-on-rails - 希望能够根据两个不同的值进行多行插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29988105/

相关文章:

ruby-on-rails - 如果满足条件则返回数组中的一个元素

testing - Rails 4 : db:test:prepare says it is deprecated, 但没有它测试无法工作

ruby-on-rails - 请求卡在 PG::Connection#async_exec

ruby-on-rails - "/admin"加载 View 后从 URL 中消失(Rails)

ruby-on-rails - 设计忘记密码不起作用

ruby-on-rails - Rails 参数人口

ruby-on-rails-4 - Arel:来自 Arel::SelectManager 与连接的事件关系

ruby-on-rails - 托管高流量的 Facebook 应用程序(游戏)

ruby - 错误 : Failed to build gem native extension on Windows

ruby - 如何运行一系列检查?