views:

46

answers:

2

I have a column type date (shown from annotate) on my Contacts table:

#  date_entered       :date(255)

This is the line of code that has worked for me locally on my sqlite3 database, but now generates an error in Heroku:

<%= contact.date_entered.to_s(:long) %>

The error that I get is:

wrong number of arguments (1 for 0)

I removed the :long component and it appears to work, but now, of course, looks badly formatted.

How do I address this?

A: 

Use

date.strftime('%m/%d/%Y')

for date formatting.

Also you can override to_s method for Date/Time to use I18n localization:

class Date
  def to_s format = :default
    I18n.l(self, :format => format)
  end
end

In this case can use both :logn (:default, :short) and '%d %B, %Y' formats.

UPD: Also it makes sense to store old method and call it in case you don't pass any params:

class Date
  alias :to_s :native_to_s

  def to_s format = nil
    format.nil? ? 
      self.native_to_s : 
      I18n.l(self, :format => format)
  end
end
fantactuka
do you know why :long doesn't work in this instance?
Angela
Date.to_s just don't have any arguments by default. (http://ruby-doc.org/core/classes/Date.html#M000685)But as I wrote above it could be overriden
fantactuka
outside of overriding...why would you suggest using date.strftime? Just more granulary? I'd have to repeat it every time though in the controller?
Angela
I suggested strftime method since it was designed for date formating, so this case (with strftime) - yes, you'll have to repeat it every time. It should be OK if you need it rarely, otherwise you'd better override to_s method. I've updated code a little - take a look at the my answer.
fantactuka
okay, cool, this is good...interesting to override it. I would just put it into a date.rb file in lib?
Angela
Y, it could be put there
fantactuka
A: 

Is that actually a postgres date field?

end_date           | date   

is what postgres has as a date field, can you check on the table itself? Also sticking contact.date_entered.class somewhere in a view to find out what the exact Ruby class is.

I have a huge hunch that it is not a timestamp or date field. Most likely actually a text or varchar and thus ActiveRecord interprets it that way.

Omar Qureshi
It is supposed to be one, It is listed in web agile with rails v 3....
Angela
Can't say for sure without having a look myself, have you found out what the class of the "timestamp" you are trying to display is?
Omar Qureshi
hmmm....it all looks like it is class Date for both.....
Angela