A model is seeded with a remote url for an image, meaning the db entry it is not created in Rails. Then the first time it is fetched from the DB in Rails I want to detect that the image has not been uploaded, assign the remote_seed_url for the image url and save! to trigger the CarrierWave upload. I want to do this only once, obviously, but the code below sometimes uploads the same image more than once.
class Item
include Mongoid::Document
include Sunspot::Mongoid2
field :image, type: String
field :remote_seed_url, type: String #URL for image
mount_uploader :image, ImageUploader
end
Then in a controller
def show
@ item = Item.find(params[:id])
# if CarrierWave has already uploded then do nothing
if !@item.image?
@item.image = @item.remote_seed_url # next save will trigger the upload?
@item.save!
end
respond_to do ...
end
The value of @item.image is usually "new" or "old" and @item.image? sometimes returns false when I can see that it has already uploaded the image. This causes the above code to upload multiple times.
- Is the controller code correct to get the image uploaded only once?
- Is there some way I might have messed things up and caused @item.image? to return false when it should be true? Maybe image aging?