Rails: a paper clip question about column names

I have two scenarios for using Paperclip, but I'm not sure how to configure the settings, or if this is possible or even necessary. You need the advice of more experienced professionals on this.

Firstly, I have a document model for downloads such as PDF files that will be defined:

has_attached_file :document...

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

Secondly, I have Gallery.rb, which has a lot of Picture.rb. The same scenario is here. Can I avoid @ picture.picture_file_name? Or is this something that should really be missed with the benefits that Piperklip gives.

Thanks in advance for any input.

+3
source share
3 answers

I accept this: the actual document (PDF file) does not match the document (which contains the physical document plus metadata). Therefore, it makes sense to see the Paperclip attachment as an attribute of the model and call its methods after the attribute and not work with the model entry itself.

One of my applications has a model Documentwith an attached file, and I just named the attribute attachment.

If this is too much inconvenience for you, you can 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
+3
source

The Paperclip gem requires three attributes of a related object.

  • attribute_file_name
  • attribute_file_size
  • attribute_file_type

attributeOf course, if the name of your file is this has_attached_file :attributewhich is usually called an image, image, etc.

, , :)

+1

Here are the methods I had to create:

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

Source: https://habr.com/ru/post/1773449/


All Articles