# New entry in table for each date between two dates?

**URL:** <https://www.ruby-forum.com/t/new-entry-in-table-for-each-date-between-two-dates/125853>\
**Category:** Rails\
**Created:** [January 12, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/new-entry-in-table-for-each-date-between-two-dates/125853 "2008-01-12T21:21:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [January 12, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/new-entry-in-table-for-each-date-between-two-dates/125853/1 "2008-01-12T21:21:38Z")

</div>

I’m having trouble wrapping my head around something I’m trying to  
do…

Basically I’m trying to build a reservations system. I have my form  
populating the reservations table with the information I need, but  
also upon creating a new reservation I need it to also populate a  
bookingstatus table. What I’m trying to figure out right now is how  
to create the code in the controller to take my checkindate and  
checkoutdate and create entries in the bookingstatus table for those  
dates and each date in between. I know it’ll be a loop of some sort,  
but I’m new to all this and can’t wrap my head around it.

Any guidance on what this controller code will look like?

Much appreciated!

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [January 13, 2008, 12:32am UTC](https://www.ruby-forum.com/t/new-entry-in-table-for-each-date-between-two-dates/125853/2 "2008-01-13T00:32:40Z")

</div>

First, creating booking statuses for every day between two dates sounds  
like  
business logic to me; thus, I’d put the behavior for it in the  
BookingStatus  
model instead of the controller.

That said, how about making the method take a range of dates. Then, you  
could iterate the range, creating a record for each one.

class BookingStatus \< ActiveRecord::Base

class \<\< self

```
def fill(date_range)
  date_range.each do |date|
    create(:date => date, ...) # with other attributes as required
  end
end

```

end

end

I’d try to pick better names for the method and its parameter, but I  
hope  
that sparks some ideas.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [January 13, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/new-entry-in-table-for-each-date-between-two-dates/125853/3 "2008-01-13T17:33:25Z")

</div>

That makes sense except I may need just a little more hand holding  
with the date\_range part. In my view I have two text fields: one for  
start\_date and one for end\_date. How do take those two dates and make  
them work for the code above? In other words how do those two dates  
become date\_range so that they can be looped through? Thanks so much!

On Jan 12, 6:32 pm, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [January 13, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/new-entry-in-table-for-each-date-between-two-dates/125853/4 "2008-01-13T22:42:08Z")

</div>

On Jan 13, 2008 11:32 AM, Jeremy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That makes sense except I may need just a little more hand holding  
> with the date\_range part. In my view I have two text fields: one for  
> start\_date and one for end\_date. How do take those two dates and make  
> them work for the code above? In other words how do those two dates  
> become date\_range so that they can be looped through? Thanks so much!

I’m sure there are as many ways to do it as there are members of this  
list!  
🙂

I’m not far enough along in my Rails experience to know which way to  
choose  
and why, but here are a couple of ideas that spring to mind.

To convert your strings to dates, you could do “1/14/2008”.to\_date, or  
you  
could do Date.parse(“1/14/2008”). Note that using a four-digit year is  
recommended for those two approaches; otherwise, you end up with a date  
whose year is 0008. If you need to support more flexible date/time  
parsing,  
I would recommend looking at Chronic [[http://chronic.rubyforge.org/](http://chronic.rubyforge.org/)].

Once you have the two dates you can just create a range, e.g.,

date\_range = (first\_date…second\_date)

If you haven’t already, you can try these things in IRB or the console  
for  
your Rails app.

I’d highly recommend spending as much time as you can learning Ruby. The  
more I learn it, the easier working with Rails becomes. Just find  
something  
that interests you, such as ranges (of integers, dates, etc.) and play  
with  
them in IRB or the Rails console. Or, write unit tests or specs (using  
RSpec  
[[http://rspec.info](http://rspec.info)]) to gain a feel for how they work. If you like, I  
could  
share a few that I wrote.

Regards,  
Craig
