# Nuby - about date\_select, getting into DB

**URL:** <https://www.ruby-forum.com/t/nuby-about-date-select-getting-into-db/92965>\
**Category:** Rails\
**Created:** [April 26, 2007, 3:34am UTC](https://www.ruby-forum.com/t/nuby-about-date-select-getting-into-db/92965 "2007-04-26T03:34:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [April 26, 2007, 3:34am UTC](https://www.ruby-forum.com/t/nuby-about-date-select-getting-into-db/92965/1 "2007-04-26T03:34:46Z")

</div>

Hello Everyone,

I am trying to use the date\_select() form helper method per AWDWROR.  
In  
the DB the column “entry\_date” is a datetime.

So using the below form, in part

============ app/views/entry/index.rhtml ===

 date \<%= date\_select("entry\_date", :start) %\>

how do I get it into the AR instance?

============ app/controllers/EntryController.rb =======  
def create  
@entry = Entry.new(  
:entry\_date =\> params[:entry\_date][“start”],  
:task\_id =\> params[:task][:id],  
:description =\> params[:description]  
);  
end

This gives me “entry\_date cannot be null” from mysql.

The backtrace/debug dump on the error page mentioned that  
Parameters: …  
“entry\_date”=\>{“start(1i)”=\>“2007”, “start(2i)”=\>“4”,  
“start(3i)”=\>“25”}

where did that (1i), (2i) stuff come from? what does it mean?

In any case I was hoping to use a DHTML calendar pop-up for this date  
entry, but wanted to get this working for the prototype. So if you  
can  
help I appeciate it

thanks  
Sam

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [April 26, 2007, 4:38am UTC](https://www.ruby-forum.com/t/nuby-about-date-select-getting-into-db/92965/2 "2007-04-26T04:38:52Z")

</div>

# ----- View

# Editing book

\<%= error\_messages\_for :book %\>

\<% form\_for(:book, :url =\> book\_path(@book), :html =\> { :method  
=\> :put }) do |f| %\>

**Created at**  
 \<%= f.datetime\_select :created\_at %\>

**Title**  
 \<%= f.text\_field :title %\>

... ... ... 

\<%= submit\_tag "Update" %\>

\<% end %\>

\<%= link\_to ‘Show’, book\_path(@book) %\> |  
\<%= link\_to ‘Back’, books\_path %\>

# ---- Controller

# POST /books

# POST /books.xml

def create  
@book = Book.new(params[:book])

```
respond_to do |format|
  if @book.save
    flash[:notice] = 'Book was successfully created.'
    format.html { redirect_to book_url(@book) }
    format.xml { head :created, :location => book_url(@book) }
  else
    format.html { render :action => "new" }
    format.xml { render :xml => @book.errors.to_xml }
  end
end

```

end

P.S. If you would prefer to let Rails completely handle the entry\_date  
for you just change the name of the column in the DB to created\_at  
(for datetime) or created\_on (for date). Then you can remove the  
date\_select entirely from the form and Rails will automatically set  
the field to the current date and time when the record is created.  
You can also use updated\_on and updated\_at to create auto updating  
date and time stamps.

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [April 26, 2007, 5:28am UTC](https://www.ruby-forum.com/t/nuby-about-date-select-getting-into-db/92965/3 "2007-04-26T05:28:54Z")

</div>

Thanks - maybe I should state the bigger picture of what I am trying  
to do - its basically an event logger. Really this is pretty simple  
but I am new at Ruby and Rails so life is hard now.

So users can choose a date on which the event occurred, that is taken  
care of my the date\_select field (for now)

then they need to choose a start and end time within that date. So for  
this I am using text fields because its easier for them to type. But  
in the DB I would like to save these as timestamp fields because there  
is going to be a lot of reporting and statistics that goes on based on  
the data.

# So in the DB I have

# mysql\> desc entries; … | entry\_date | date | NO | | | | | start\_time | timestamp | YES | | NULL | | | end\_time | timestamp | YES | | NULL | | …

and in the controller, I am getting the entry\_date in OK as you  
indicated. But to get a timestamp for the start\_time and end\_time, I  
need to

1. get the date from params[:entry\_date] as a Date object (I think) at  
time 00:00?
2. add the given hours and minutes to the Date
3. convert that to a unix\_timestamp which mySQL will be happy with

so far I am stuck on step 1 😉

step 3 might not be needed, I’m not sure yet.

Any pointers? how would the seasoned pro go about this?

thanks  
Sam

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [April 26, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/nuby-about-date-select-getting-into-db/92965/4 "2007-04-26T21:35:27Z")

</div>

> Any pointers? how would the seasoned pro go about this?

Well, a seasoned pro would likely use a JavaScript/DHTML date and time  
picker with all the fancy bells and whistles. Then use it’s resulting  
date string representation to parse into a Ruby Time object. That is  
if they choose not to use the build in date time selectors Rails  
provides.

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [April 26, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/nuby-about-date-select-getting-into-db/92965/5 "2007-04-26T22:58:38Z")

</div>

On Thursday 26 April 2007, Robert W. wrote:

> > Any pointers? how would the seasoned pro go about this?
> 
> Well, a seasoned pro would likely use a JavaScript/DHTML date and time  
> picker with all the fancy bells and whistles. Then use it’s resulting  
> date string representation to parse into a Ruby Time object. That is  
> if they choose not to use the build in date time selectors Rails  
> provides.

well I was going to use a DHTML timepicker widget but thought  
prototyping with this would be easier. But I have since found out, not  
so much.

my current creation looks like this for the curious:  
[http://pastecode.com/28881](http://pastecode.com/28881)
