【发布时间】:2015-07-11 08:35:43
【问题描述】:
是否可以根据两个不同的列值为一条记录创建多行?
示例:在我的模型条目表中,我有一个名为 full_range 的列,这将在 leave_start 和 leave_end 日期之间选择日期,例如,假设我选择 05/01/15 作为我的 @ 987654325@ 日期和05/05/15 作为我的leave_end 日期,这将给我05/01/15, 05/02/15, 05/03/15, 05/04/15, 05/05/15 我的full_range。
那么对于我的另一个名为range_days 的列,值将是5,这是因为我在05/01/15 和05/05/15 之间有5 天。
我想做的是根据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
这里还有我的入口控制器。
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
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord