プロファイルとドキュメント テーブルを含む添付ファイル モデルとの多態的な関連付けがあります。attachment.rb に次のコードを含めました。
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
has_attached_file :attach,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:url => ":s3_domain_url",
:path => "/contents/:id/:basename.:extension"
validates_attachment_content_type :attach,
:content_type => ['application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/pdf',
'image/jpeg', 'image/jpg', 'image/png']
end
そして私のprofile.rbで
class Profile < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
私のdocument.rbで
class Document < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
私の要件は、プロファイルを保存するときに画像形式のみを検証し、ドキュメントを保存するときにアプリケーション形式のみを検証することです。これを解決する方法を教えてください。