I am having difficulty using carrierwave together mongoid. I am writing an application that requires images to be stored in a number of collections, so I have created the Image class as follows (please excuse the use of imageable):
class Image
include Mongoid::Document
embedded_in :imageable, polymorphic: true
mount_uploader :file, ImageUploader
end
ImageUploader is as created by rails generate uploader Image.
Then I have a story model:
class Story
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
field :content, :type => String
field :category, :type => String
embeds_many :blog_images, as: :imageable
end
When I try to create a Story object, i get the error uninitialized constant BlogImage even if I do not supply and image through the form. I have also tried in the console to call story.blog_images << Image.new with the same error.
In my gem file I have:
gem 'carrierwave'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
My versions are Mongoid – 2.4.7 and carrierwave – 0.6.2.
My main question is how to fix this error, but I am also not too sure about how to set up the form to receive these images. (Ideally I’d like to use javascript to add a new dialog, each time a file is selected.) Is there something wrong with the way I have set up my models or am using carrierwave?
You will need to specify the target class for blog_images because it cannot be derived from the name of the relation. Try: