Undefined Method 'to_i' on [datetime]

273 views
Skip to first unread message

kikito

unread,
Aug 30, 2009, 11:22:54 AM8/30/09
to Paperclip Plugin
Hi there,

I'm getting an error when presenting a picture attached to my model.

The line seems to be originating on line 190 of paperclip/lib/
attachment.rb

def updated_at
time = instance_read(:updated_at)
time && time.to_i # here
end

In my case, updated_at is a datetime. And those don't have a to_i
method.

Removing the && time.to_i part seems to fix the issue.

As a reference, here's my migration and model:

class CreatePictures < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
t.references :user
t.string :file_file_name
t.string :pictures, :file_content_type
t.ingeger :file_file_size
t.datetime :file_updated_at

t.timestamps
end
end

def self.down
drop_table :pictures
end
end

class Picture < ActiveRecord::Base

PICTURE_STYLES = {
:original => ['640x480>', :jpg],
:medium => ['256x192>', :jpg],
:small => ['128x96>', :jpg]
}
has_attached_file :file,
:styles => Picture::PICTURE_STYLES
attr_protected :file_file_name, :file_content_type, :file_size

#other non-paperclip stuff here
end

I'm using paperclip 2.3.1.

Iain

unread,
Sep 1, 2009, 1:03:34 AM9/1/09
to Paperclip Plugin
Just to add my support, I'm getting the same error on paperclip 2.3.1
on OS X with Ruby 1.8.7

Iain

unread,
Sep 1, 2009, 7:06:44 PM9/1/09
to Paperclip Plugin
I've found a solution - I was using the plugin version of paperclip
but I just changed to the gem and now "it just works"! :-)

Iain

unread,
Sep 2, 2009, 12:02:28 AM9/2/09
to Paperclip Plugin
I've had a deeper look and it seems that the current version in github
is more recent than the gem version, and that in the github version
the default behaviour of the Attachment.url method has been changed to
append the date-time to the filename. However, the files aren't saved
with a datetime on the the end (by default) so the url that is
generated doesn't exist.

It seems like there are two solutions (long term):
* Change the default behaviour back to not including the datetime (I
think this one is most sensible as otherwise existing code will break
on upgrading to the latest version)
* Change the default interpolation for the file path to include the
datetime on the end of the filename (much cooler, but existing
projects will still be broken)

Iain

unread,
Sep 2, 2009, 12:08:18 AM9/2/09
to Paperclip Plugin
Oh, and that datetime.to_i doesn't always work!

Iain

unread,
Sep 2, 2009, 12:17:27 AM9/2/09
to Paperclip Plugin
I've raised an issue on github. (#85)

Jonathan Yurek

unread,
Sep 3, 2009, 2:20:59 PM9/3/09
to papercli...@googlegroups.com
I've fixed this issue and closed the github ticket. The fix for this
is in master. DateTime may not have a to_i method, but it does have a
to_f method.
--
Jonathan Yurek, Founder and CTO
thoughtbot, inc.
organic brains. digital solutions.

617.482.1300 x114
http://www.thoughtbot.com/

Enrique Garcia Cota

unread,
Sep 4, 2009, 1:59:33 AM9/4/09
to papercli...@googlegroups.com
Well, thanks a lot for this!

I will try the new version asap.

Regards,

Enrique
Reply all
Reply to author
Forward
0 new messages