2

I have a model named CustomFields that belongs to a main class (that has_many :custom_fields). This model has the attributes contents and datatype. I want the contents to be either a string or a uploader using carrierwave, according to the datatype of the object. I've done the following:

class CustomField < ActiveRecord::Base

  after_initialize :set_uploader 

  def set_uploader
    if self.datatype == 'file'
      CustomField.mount_uploader :contents, ImageUploader
    end
  end 

end

It's not working because it's turning all the object's contents into uploaders, not only the 'file' datatype. How can I solve that?

Ricardo Nacif
  • 508
  • 4
  • 12
  • 1
    You could just use two different fields to store the information, and dynamically decide which one to display with a common reader method. – Thomas Klemm May 27 '14 at 21:30

1 Answers1

-1

Just a guess:

class CustomField < ActiveRecord::Base

  mount_uploader :image, ImageUploader, if: :file?

  def file?
    self.datatype == 'file'
  end
end
Diego Polido Santana
  • 1,425
  • 11
  • 19