# Error trying to save DateTime object in DataBase

**URL:** <https://www.ruby-forum.com/t/error-trying-to-save-datetime-object-in-database/147775>\
**Category:** Rails\
**Created:** [September 5, 2008, 9:13am UTC](https://www.ruby-forum.com/t/error-trying-to-save-datetime-object-in-database/147775 "2008-09-05T09:13:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mrbrett](https://www.ruby-forum.com/u/mrbrett)\
**Post date:** [September 5, 2008, 9:13am UTC](https://www.ruby-forum.com/t/error-trying-to-save-datetime-object-in-database/147775/1 "2008-09-05T09:13:05Z")

</div>

Hello I am having trouble with datetime objects, I had the “good” idea  
of making a form, asking for the parameters of DateTime and then give  
this object to the Database, apparently this is not the way.

I have this error: wrong number of arguments (2 for 1)

And this is what I tried:

I have a model like this  
datetime :when  
string :where  
A view like this  
\<% form\_tag exams\_path do %\>

Place:  
 \<%= text\_field\_tag 'place' %\>

Date and Hour:  
 Day: \<%= text\_field\_tag 'day', nil, :size =\> 4, :maxlength =\> 2 %\>  
 Month: \<%= text\_field\_tag 'month',nil, :size =\> 4, :maxlength =\> 2 %\>  
 year: \<%= text\_field\_tag 'year', nil, :size =\> 4, :maxlength =\> 4 %\>  
 Hour: \<%= text\_field\_tag 'hour', nil, :size =\> 4, :maxlength =\> 2 %\>  
 Minute: \<%=text\_field\_tag 'minute',nil, :size =\> 4, :maxlength =\> 2%\>

\<%= submit\_tag 'Save' %\>

\<% end %\> And a controller like this @fecha = DateTime.new(params[:year].to\_i, params[:month].to\_i, params[:day].to\_i, params[:hour].to\_i, params[:minute].to\_i) @exam = Exam.new(DateTime.now, params[:place]) if @exam.save redirect\_to exams\_path else render :action =\> 'new' end

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![mrbrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mrbrett](https://www.ruby-forum.com/u/mrbrett)\
**Post date:** [September 5, 2008, 9:20am UTC](https://www.ruby-forum.com/t/error-trying-to-save-datetime-object-in-database/147775/2 "2008-09-05T09:20:38Z")

</div>

On Sep 5, 8:13 am, Aldo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> And a controller like this  
> @fecha = DateTime.new(params[:year].to\_i, params[:month].to\_i,  
> params[:day].to\_i, params[:hour].to\_i, params[:minute].to\_i)  
> @exam = Exam.new(DateTime.now, params[:place])

Assuming Exam is an activerecord object you should be passing a hash  
of attributes (eg :foo =\> params[:foo], :bar =\> params[:bar], whereas  
you are passing 2 arguments

Fred

---

<div class="post-metadata">

**Author:** ![mrbrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mrbrett](https://www.ruby-forum.com/u/mrbrett)\
**Post date:** [September 8, 2008, 6:09am UTC](https://www.ruby-forum.com/t/error-trying-to-save-datetime-object-in-database/147775/3 "2008-09-08T06:09:59Z")

</div>

I didn’t understand your solution Fred, thanks a lot for your post. I  
solved my problem by saving in the database each element of the date as  
integers, then I read this elements and I build my DateTime element.

---

<div class="post-metadata">

**Author:** ![mrbrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mrbrett](https://www.ruby-forum.com/u/mrbrett)\
**Post date:** [September 8, 2008, 9:12am UTC](https://www.ruby-forum.com/t/error-trying-to-save-datetime-object-in-database/147775/4 "2008-09-08T09:12:40Z")

</div>

On Sep 8, 5:09 am, Aldo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## I didn’t understand your solution Fred, thanks a lot for your post. I solved my problem by saving in the database each element of the date as integers, then I read this elements and I build my DateTime element.
> 
> Posted viahttp://www.ruby-forum.com/.

I was just saying that you’ve passed two parameters to Exam.new rather  
than a hash of options.  
Fred
