Multiple clip models using STI

Purpose: One Rails model (table) with several models inherited by it, each of which defines its own paperclip configurations has_attached_file.

I used to have one load class that I used with paperclip. The problem is that as additional types of files (pdf files, text documents, etc.) are added, they are still processed as images through the "style" and "convert_options". Also, now I need to have some files stored on S3 and others stored locally.

However, I restructured such things that I now have S3File, Contract, and other models that inherit from Upload, which is still inheriting from ActiveRecord :: Base.

# app/models/project.rb
class Project < ActiveRecord::Base
  has_many :contracts, :dependent => :destroy
    accepts_nested_attributes_for :contracts, :allow_destroy => true

  has_many :s3_files, :dependent => :destroy
    accepts_nested_attributes_for :s3_files, :allow_destroy => true
  # ...
end

# app/models/upload.rb
class Upload < ActiveRecord::Base
  belongs_to :project
end

# app/models/contract.rb
class Contract < Upload
  has_attached_file :item,
    :url  => "/projects/:project_id/uploads/:id/:basename.:extension",
    :path => ":rails_root/public/:class/:attachment/:id/:basename.:extension"

  do_not_validate_attachment_file_type :item
end

# app/models/s3_file.rb
class S3File < Upload
  has_attached_file :s3file,
    storage: :s3,
    url: ':s3_domain_url',
    path: lambda {|u| 'files/:basename.:extension' }

  do_not_validate_attachment_file_type :s3file
end

, , Upload, S3File Contract.

irb(main):005:0> Project.first.contracts
=> #<Upload id: 14833, project_id: 9717, upload_type: "private", item_file_name: "abcd.pdf", item_category: "contracts", item_content_type: "application/pdf", item_file_size: 671367, rake_processed: 0, name: "", created_at: "2013-05-30 20:05:02", updated_at: "2013-05-30 20:05:02">

, . , URL, has_attached_file.

paperclip, " " ActiveRecord:: Base, - , , .

:

  • ? , , .
  • ? ? , , S3File - , .
  • has_attached_file (: item : s3file) , ?
+4
1

STI paperclip # 293, # 601, # 605 .

STI ?

, . project.contracts Upload, Rails. class_name: "Contract has_many , :

:

, Relation . , :

 class Blog < ActiveRecord::Base   
     has_many :published_posts, -> { where published: true }, class_name: 'Post' 
 end 

. db, , . . .

has_attached_file

, , . , , , URL- .

+2

All Articles