Devguru
Saving embedded attachments with Mongoid and Carrierwave

While developing our latest application, we discovered that Mongoid doesn’t save embedded objects when their parent is saved. On the GitHub page of this issue there were suggested some solutions and it appears that one posted by mcasimir works very well. What happened to be the next problem is that with this solution comes the issue with saving attachments’ filenames. We’ve found a solution somewhere on GitHub and changed it a little so it works with overriding filenames :

 
  after_validation :extract_filename

  def extract_filename
    img_original_filename = img.instance_variable_get("@original_filename")
    if self.img.class.filename and img_original_filename.present?
      self.img_filename = self.img.class.filename
    elsif img_original_filename.present?
      self.img_filename = img_original_filename
    end
  end