Reputation: 4038
I am planning to use single table inheritance using the Paperclip gem that would be dynamic based on the content type.
class Document < ActiveRecord::Base
has_attached_file :file, photo_options #if content type is an image
has_attached_file :file, pdf_options #if content type is a pdf file
end
class Photo < Document
# photo specific code
end
class Pdf < Document
# pdf specific code
end
Is it possible to have the has_attached_file
be dynamic based on the content type? One use case would be for when trying to create a new instance of Document
from a file form upload:
@document = Document.new params[:document]
I hope my question makes sense. Thanks.
Upvotes: 0
Views: 159
Reputation: 494
You can do it like:
class Document < ActiveRecord::Base
end
class Photo < Document
has_attached_file :file, photo_options #if content type is an image
# photo specific code
end
class Pdf < Document
has_attached_file :file, pdf_options #if content type is a pdf file
# pdf specific code
end
class DocumentsController < ApplicationController
#Assuming is the new method.
def new
@document = params[:document_type].classify.safe_constantize.new
end
end
And use @document in your form.
Upvotes: 1