# Mysql Date field not getting set correctly

**URL:** <https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791>\
**Category:** Rails\
**Created:** [August 8, 2006, 12:43am UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791 "2006-08-08T00:43:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 12:43am UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/1 "2006-08-08T00:43:28Z")

</div>

I created a simple test to try and pinpoint my problem but nothing seems  
clear to me.

I created a Thing model with fields { id (INT), date (DATE) }

now when I try to create a new “thing” the date does not get stored, and  
no errors are reported.

@myNewThing = Thing.new(:date =\> “2006-08-07”)  
@myNewThing.save

this results in a new record with a date of “0000-00-00”

what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 3:04am UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/2 "2006-08-08T03:04:38Z")

</div>

Hi,  
I try to receive a list of remote files on a FTP-server. I have written  
a Script that doesn’t work with WindowsXP but with Linux.  
Here’s the code that asks for the filelist and puts the filenames on the  
console:

* * *

ftp = Net::FTP.new(WMV\_ADDRESS)  
ftp.login(WMV\_USER, WMV\_PASS)

ftp.chdir(“otr04”)

#Doesn’t work  
files = ftp.nlst

files.each do |file|  
puts file  
end

#Works  
#puts ftp.status

ftp.close

* * *

The Problem is that the methods ‘nlst’ and ‘list’ behave strangely with  
WindowsXP. They don’t return so the script never stops. On my Linux  
machine the same script works perfectly.

Has anyone an idea how to get this working?

Regards,  
Tim

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 2:37am UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/3 "2006-08-08T02:37:36Z")

</div>

Todd,

I suspect your using MySQL and it can’t convert the data format  
correctly.

Try this:

@myNewThing = Thing.new(Time.now.to\_s(:db))  
@myNewThing.save

Does it take it in that format?

Zack

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 3:28am UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/4 "2006-08-08T03:28:04Z")

</div>

Zack C. wrote:

> Todd,
> 
> I suspect your using MySQL and it can’t convert the data format  
> correctly.
> 
> Try this:
> 
> @myNewThing = Thing.new(Time.now.to\_s(:db))  
> @myNewThing.save
> 
> Does it take it in that format?

Well, I’m using Date in MySQL not DateTime so here’s what I get…

Strangely this does not error, but it also still inserts “0000-00-00” :  
@myNewThing = Thing.new(:date =\> Time.now.to\_s(:db))  
=\> #\<Thing:0x26deaec @new\_record=true, @attributes={“date”=\>“2006-08-07  
18:20:06”}\>

I would expect I need to use Date not Time but this actually results in  
an error.

```
@myNewThing = Thing.new(:date => Date.today.to_s(:db))
ArgumentError: wrong number of arguments (1 for 0)
    from (irb):8:in `to_s'
    from (irb):8

```

This next approach does not error but again I only get “0000-00-00”  
@myNewThing = Thing.new(:date =\> Date.today(:db))  
=\> #\<Thing:0x2642f0c @new\_record=true, @attributes={“date”=\>#\<Date:  
4907909/2,0,db\>}\>

I don’t remember having these problems in the past.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 11:02am UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/5 "2006-08-08T11:02:11Z")

</div>

“Todd S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> > Does it take it in that format?
> 
> Well, I’m using Date in MySQL not DateTime so here’s what I get…
> 
> Strangely this does not error, but it also still inserts “0000-00-00” :  
> @myNewThing = Thing.new(:date =\> Time.now.to\_s(:db))  
> =\> #\<Thing:0x26deaec @new\_record=true, @attributes={“date”=\>“2006-08-07  
> 18:20:06”}\>

I suspect there is some other error, what is the return value of

@myNewThing.save

also check @myNewThing.errors.inspect (look for other such functions).

Some validation might be failing.

Use script/console (in case you are not) to debug this.

## HTH.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| Great wits are sure to madness near allied,  
| And thin partitions do their bounds divide.  
|  
| (John Dryden, Absalom and Achitophel, 1681)  
`----

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/6 "2006-08-08T19:36:43Z")

</div>

Todd S. wrote:

> I created a simple test to try and pinpoint my problem but nothing seems  
> clear to me.
> 
> I created a Thing model with fields { id (INT), date (DATE) }
> 
> now when I try to create a new “thing” the date does not get stored, and  
> no errors are reported.
> 
> @myNewThing = Thing.new(:date =\> “2006-08-07”)  
> @myNewThing.save
> 
> this results in a new record with a date of “0000-00-00”
> 
> what am I doing wrong?

You many want to try:

Thing.new(:date =\> Date.today)

or

Thing.new(:date =\> Time.now)

The typecasting should happen for you.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 1:23pm UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/7 "2006-08-08T13:23:29Z")

</div>

On Tuesday, August 08, 2006, at 3:28 AM, Todd S. wrote:

> > 
> 
> an error.
> 
> I don’t remember having these problems in the past.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

By any chance did you alter your default date format for Rails? If you  
try to write a date field with anything other than the default format  
(i.e., 2006-08-08), it will silently fail and just write ‘0000-00-00’ to  
the database field.

If you altered the default date output in your environment, turn that  
off and see if it works now. Otherwise print out the contents of the  
field you are trying to write and see if it is misformatted.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 9:29pm UTC](https://www.ruby-forum.com/t/mysql-date-field-not-getting-set-correctly/101791/8 "2006-08-08T21:29:26Z")

</div>

> By any chance did you alter your default date format for Rails? If you  
> try to write a date field with anything other than the default format  
> (i.e., 2006-08-08), it will silently fail and just write ‘0000-00-00’ to  
> the database field.
> 
> If you altered the default date output in your environment, turn that  
> off and see if it works now.

Thank you!!!

Although I’m not sure why it still dod not work when I used Time.  
When I remove my Date class everything works as expected.

Thank you all very much.

I had this in my application.rb file…

class Date  
def to\_s  
strftime(’%m / %d / %Y’)  
end  
end
