2017-03-01 15 views
0

私は、S3でPaperclipを使用してファイルを保存していますが、ユーザーがアプリケーションから入力した画像を保存していますが、 、ここで問題を説明するための私のコードは次のとおりです。ActiveRecord specific selectはpaperclip urlを持っていません

私がデータベース(file_nameに、FILE_SIZE、file_updated_at、file_content_typeにファイルを格納すべてのフィールドを選択する必要があるように思わペーパークリップのURLジェネレータを見てみると
class UploadedFile < ActiveRecord::Base 
    has_attached_file :file 
    attr_accessor :file_url 

    def file_url 
    self.file.url(:original) if self.file 
    end 
end 

UploadedFile.first.as_json(methods: [:file_url]) #Will work and will show the url in the json 

UploadedFile.all.as_json(methods: [:file_url]) #Will work and will show the url in the json 

UploadedFile.select('id', 'othertablecolumn').joins("JOINS SOMETING").where("SOME CONDITIONS").as_json(methods: [:file_url]) #Will not work and will show the missing url in the json 

答えて

0

):

:hash_data=>":class/:attachment/:id/:style/:updated_at 

を使用すると、URL全体を構築できます。

関連する問題