Search code examples
ruby-on-railsruby-on-rails-3activerecordruby-on-rails-3.1carrierwave

Carrierwave and Activerecord Multiple Queries


I finally diagnosed a problem I have been having querying a table in activerecord. Instead of performing 1 query with Model.all it was calling each one separately. The bug is fixed when I comment out

  mount_uploader :image, ImageUploader
  mount_uploader :home_image, HomeImageUploader

For some reason mount_uploader is calling each class instance again and slows down my queries. Has anyone had a similar problem? I saw in the carrierwave github docs to add

require 'carrierwave/orm/activerecord'

to my model class but that did not help anything. Also, confused about this line in the carrierwave docs, "Make sure you are loading CarrierWave after loading your ORM". Any suggestions would be much appreciated, thanks.


Solution

  • I finally tracked down the bug, all of these extra queries were taking place because of how I setup my store_dir in my uploader

     "images/#{Rails.env}/#{model.class.to_s.underscore}/#{model.class.find(model.id).name}"
    

    It kept loading the model to get its name. I changed it to

    "images/#{Rails.env}/#{model.class.to_s.underscore}/#{model.id}"
    

    and it solved my querying problem. I hope this helps someone else down the road.