开发者

Rails: Paperclip question regarding column names

I have two scenarios for using Paperclip, but I'm unsure of how to tweak the settings or if it's possible or even necessary. Need the advice of more seasoned professionals on this one.

First up, I have a Document model for uploads such as PDFs, which would be defined:

has_attached_file :document...

This would give me column names like @document.document_file_name. Anyway that I could have @document.file_name instead?

Secondly, I have Gallery.rb which has many Picture.rb. Same scenario here as well. Can I avoid having @picture.picture_file_name? Or is this something that should really be overlooked with the gains that Paperclip affords.

Thanks in advance for开发者_StackOverflow中文版 any input.


My take on this: The actual document (PDF file) is not the same as a document record (which comprises the physical document plus metadata). Therefore it makes sense to see the Paperclip attachment as an attribute of the model and have its methods be called after the attribute, and not operate on the model record itself.

One of my apps has a Document model with an attached file too, and I simply called the attribute attachment.

If this is too much of an inconvenience for you, you could always implement your own getters in the model:

class Document < ActiveRecord::Base
  has_attached_file :attachment    # ... or whatever you are calling it

  def file_name
    self.attachment.file_name
  end

  def file_size
    self.attachment.file_size
  end

  def file_type
    self.attachment.file_type
  end
end


The Paperclip gem requires three attributes on the associated object.

  1. attribute_file_name
  2. attribute_file_size
  3. attribute_file_type

attribute of course if the name of your file and it is the has_attached_file :attribute filed commonly called picture, image, whatever.

If you want to change one of those names you will need to edit the gem itself which seems crazy for just changing the attribute name :)


Here are the methods I had to create:

{attribute}_file_name
{attribute}_file_size
{attribute}_content_type
{attribute}_updated_at
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜