Checking date


#1

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


#2

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/ref_c_time.html#Time.strftime

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

Not quite sure what you’re asking here.

Mark


#3

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


#4

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


#5

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?


#6

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


#7

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.


#8

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.