4

i use rails 3.2.6, ruby 1.9.3 with the mongoid gem 3.0.

i want to display the created_at field of a database entry, but get the following error:

undefined method `getlocal' for "Wed, 25 Apr 2012 15:04:37 -0400":String

here's the rails code:

<dt>Erstellt am:</dt><dd><%= @app.created_at %></dd>

any advice what's the problem? is there a bugfix? should work in my opinion?

thanks in advance!

trnc
  • 20,581
  • 21
  • 60
  • 98
  • I think I found your answer [here](http://stackoverflow.com/questions/4123683/nomethoderror-undefined-method-getlocal-for-datetime-object). I hope it helps... – gabrielhilal Jun 28 '12 at 10:52
  • saw that before, but i don't know how to use it for the created_at variable. any advice? – trnc Jun 28 '12 at 10:56
  • try converting it to string `@app.created_at.to_s` – gabrielhilal Jun 28 '12 at 11:08
  • also take a look [here](http://stackoverflow.com/questions/279769/convert-to-from-datetime-and-time-in-ruby) – gabrielhilal Jun 28 '12 at 11:16
  • @Tronic In the console, what does `@app.created_at.class` return? Does it return this `=> ActiveSupport::TimeWithZone`, if not, why is it being stored as something else? – Hishalv Jun 28 '12 at 14:17
  • it's returning DateTime! – trnc Jul 11 '12 at 06:22
  • did you got the solution for this?? – abhas Jul 20 '12 at 20:54
  • nope. the mongoid user group wasn't helpful as well. but i dropped mongoid for now, will roll back to mysql! – trnc Jul 20 '12 at 21:03
  • Something with your data could be corrupted. That was my case too, see my previous Q/post : [ActionView::Template::Error (undefined method `getlocal' for “2008-02-14T02:20:50Z”:String):](http://stackoverflow.com/questions/11586135/actionviewtemplateerror-undefined-method-getlocal-for-2008-02-14t02205) – Luca G. Soave Aug 11 '12 at 10:13

3 Answers3

1

getlocal is a method on the Time class, so it may be a problem of intermixing object types. The system is expecting the @app.created_at to be an instance of Time, not DateTime. Make sure the field type for created_at is DateTime and that when creating/updating this field, make sure the object you put in is also a DateTime object.

ksugiarto
  • 940
  • 1
  • 17
  • 40
Carson Cole
  • 4,183
  • 6
  • 25
  • 35
1

try adding the following to your model

include Mongoid::Timestamps

see http://mongoid.org/en/mongoid/docs/extras.html#timestamps

Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51
0

if you use mongoid, instead of
@app.created_at
try
@app[:created_at]
this worked for me

Sagiv Ofek
  • 25,190
  • 8
  • 60
  • 55