Is it possible to create multiple rows for one record based off two different column values?
Example: In my table for my Model Entry I have a column called full_range
this will have the dates choose between a leave_start
and leave_end
date, so for example lets say I pick 05/01/15
as my leave_start
date and 05/05/15
as my leave_end
date, this would give me 05/01/15, 05/02/15, 05/03/15, 05/04/15, 05/05/15
for my full_range
.
Then for my other column called range_days
the value will be 5
this is because I have 5
days between 05/01/15
and 05/05/15
.
What I would like to do is split my full_range values based off range_days
and I would like to insert multiple rows for each date from my full range and I guess the range_days
would come into play to say create the value of rows to create.
Right now I only get one row like so..
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
So in theory what I would like to see in my database would be this so it looks at full_range
first and grabs the first date fills it in for full_range_date
then looks at the next and next... based of range_days
it does 5days which is 5 rows.
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
How could I go about doing this any help would be greatly appreciated!!!
I'm using rails 4.1.8
also for extra info here is my entry 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
and my entry model
class Entry < ActiveRecord::Base
self.primary_key = 'id'
end
Based off the answer given I tried this but still it only created one row what Would like it to do is create a new row for each date from 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
Just for giggles I tried just doing a raw sql statement like in my entry model
like so this inserted the correct amount of rows but all of the data was an exact match nothing changed.
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