New entry in table for each date between two dates?

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!

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

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.” [email protected]

On Jan 13, 2008 11:32 AM, Jeremy [email protected] 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!
:slight_smile:

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/].

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]) to gain a feel for how they work. If you like, I
could
share a few that I wrote.

Regards,
Craig