Mysql Date field not getting set correctly

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?

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

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

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.

“Todd S.” [email protected] 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://www.kreeti.com
Read my blog at: 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)
`----

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.

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


Rails mailing list
[email protected]
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

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