Forum: Ruby on Rails checking date

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
SIX-S (Guest)
on 2006-04-21 02:29
(Received via mailing list)
Radrails created a field for date


      <%= product.date_available.strftime("%y-%m-%d") %>
What does this mean?
Should the definition be date or datetime? I tried both and no results
Jim
Mark P. (Guest)
on 2006-04-21 02:55
SIX-S wrote:
> Radrails created a field for date
>       <%= product.date_available.strftime("%y-%m-%d") %>
> What does this mean?
That will convert the date "date_available" into a string with the
format
yy-mm-dd as described here:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/...

> Should the definition be date or datetime? I tried both and no results

Not quite sure what you're asking here.

Mark
SIX-S (Guest)
on 2006-04-21 06:36
(Received via mailing list)
Thanks for answering my question.
I dont know where the name " "strftime" originates
I described the date_available field in the table products(from tutorial
"depot") as type datetime. I changed the table type of date_available as
"date" when I had the error when running the program "list .rhtml" in my
browser Firefox.
JIm
----- Original Message -----
From: "Mark P." <removed_email_address@domain.invalid>
To: <removed_email_address@domain.invalid>
Sent: Thursday, April 20, 2006 4:55 PM
Subject: [Rails] Re: checking date
Mark P. (Guest)
on 2006-04-21 15:04
SIX-S wrote:
> Thanks for answering my question.
> I dont know where the name " "strftime" originates
> I described the date_available field in the table products(from tutorial
> "depot") as type datetime. I changed the table type of date_available as
> "date" when I had the error when running the program "list .rhtml" in my
> browser Firefox.
> JIm

What error do you have that you are trying to correct?

datetime is the type stored in the database only, in your product object
the attribute date_available will be of class Time... strftime is a
method of Time.

You may get an error when date_available is not set in a record you wish
to display/list, it will complain that no such method nil.strftime
exists.  Try the line:
 <%= product.date_available.strftime("%y-%m-%d") unless
product.date_available.nil? %>

Or logic to that effect, alternatives can be found by searching the list
archive.

Cheers,
Mark
Anonymous (Guest)
on 2006-05-07 01:38
(Received via mailing list)
I have a problem like the one described. My database has a a table
paintings with a DATETIME field called creation_date. In list.rhtml I
code:

   painting.creation_date.strftime("%y-%m-%d")

Rails complains:
You have a nil object when you didn't expect it!
The error occured while evaluating nil.strftime

I'm sure the expression painting.creation_date results in a variable of
class Time, as the expression painting.creation_date.class returns Time.

So why does Rails think that painting.creation_date is a nil object?
Anonymous (Guest)
on 2006-05-07 02:18
(Received via mailing list)
He, I found out why Rails complains about nil.strftime:
I have rows in my database with no value for the field creation_date.
So, as a matter of fact, Rails doesn't complain about errors in the
code. While running the code Rails is checking (or reading) the database
and finds nil values for the creation_date fields of some rows. That's
what is reported as nil.strftime
Adam B. (Guest)
on 2006-05-07 09:56
Anonymous wrote:
> He, I found out why Rails complains about nil.strftime:
> I have rows in my database with no value for the field creation_date.
> So, as a matter of fact, Rails doesn't complain about errors in the
> code. While running the code Rails is checking (or reading) the database
> and finds nil values for the creation_date fields of some rows. That's
> what is reported as nil.strftime

For the record, Rails has a nifty little feature where if you create a
table with a field named "created_on" it will automatically populate new
rows with the current date.
Anonymous (Guest)
on 2006-05-08 01:04
(Received via mailing list)
Thanks for the reply Adam. I knew about the feature and it is indeed
useful. My creation_date field though, wasn't about the creation of the
row but about the creation of the product itself. But I am certainly
going to use created_on.
This topic is locked and can not be replied to.