Could not set up grid_fs_ credentials #50

swistaczek opened this Issue Jul 26, 2012 · 7 comments


None yet

7 participants



require 'carrierwave/mongoid'

CarrierWave.configure do |config|
  config.grid_fs_database = 'bga'
  config.grid_fs_port = 27017
  config.grid_fs_host = ''
  config.grid_fs_username = 'asdasd'
  config.grid_fs_password = 'asda' = :grid_fs
  config.root = Rails.root.join('tmp')
  config.cache_dir = "uploads"


config/initializers/carrier_wave.rb:4:in `block in <top (required)>': undefined method `grid_fs_database=' for CarrierWave::Uploader::Base:Class (NoMethodError)
    from /Users/ernest.bursa/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/uploader/configuration.rb:97:in `configure'


gem 'carrierwave'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'


I'm trying to use the Mongoid 3.x support from:

gem 'carrierwave-mongoid', '~> 0.3.0.alpha', require: 'carrierwave/mongoid', git: 'git://', branch: 'mongoid-3.0'

In the source of grid_fs.rb

    # When you already have a Mongo connection object (for example through Mongoid)
    # you can also reuse this connection:
    #     CarrierWave.configure do |config|
    # = :grid_fs
    #       config.grid_fs_connection = Mongoid.database
    #       config.grid_fs_access_url = "/images"
    #     end

But Mongoid 3 no longer supports Mongoid.database. Instead it looks like it uses sessions, in order to support multiple sessions to multiple database connections.


"In a nutshell, a "session" is a database connection between the app and the db server. This can be a single connection, or many in the case of replica sets, but it's all encapsulated by a single session object. At minimum you just need to define 1 default session."

Mongoid configuration can be done through a mongoid.yml that specifies your options and database sessions. The simplest configuration is as follows, which sets the default session to "localhost:27017" and provides a single database in that session named "mongoid".

      database: mongoid
        - localhost:27017

So it should/could be changed to something like this I think:

CarrierWave.configure do |config| = :grid_fs
  # Same as your MongoHQ database conenction parameters
  config.grid_fs_connection = Mongoid::Config.sessions[:default] # was Mongoid.database for 2.4

Except I get this error:

carrierwave.rb:39:in `block in <top (required)>': undefined method `grid_fs_connection=' for CarrierWave::Uploader::Base:Class (NoMethodError)
    from /Users/kmandrup/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave/uploader/configuration.rb:97:in `configure'
    from /Users/kmandrup/.rvm/gems/ruby-1.9.3-p194/gems/carrierwave-0.6.2/lib/carrierwave.rb:15:in `configure'

Looking into the carrierwave config code, I can see that it includes various config modules into CarrierWave::Uploader::Base.

module CarrierWave
  module Uploader
    class Base
      include CarrierWave::Uploader::Callbacks
      include CarrierWave::Uploader::Proxy
      # ... more modules included
module CarrierWave
  module Uploader
    module Configuration
      extend ActiveSupport::Concern

      included do
        class_attribute :_storage, :instance_writer => false

        add_config :root
        add_config :base_path

In carrierwave/mongoid.rb however:

class CarrierWave::Uploader::Base
  add_config :grid_fs_connection

Looks a bit ugly and breaks the current carrierwave convention. The extra config options should instead be put in a separate module IMO and then included into CarrierWave::Uploader::Base.


I guess this would be a nice solution ;)


class CarrierWave::Uploader::Base
  include CarrierWave::Uploader::GridFs


module CarrierWave
  module Uploader
    module GridFs
      extend ActiveSupport::Concern

      included do
        add_config :grid_fs_access_url


      module ClassMethods
        def grid_fs_config
          configure do |config|
            config.storage_engines[:grid_fs] = "CarrierWave::Storage::GridFS"

I am running in to this issue now as well so if you need testing done on a solution just let me know.

Cheers, JB

cheeyeo commented Aug 26, 2012


i am still having the same error undefined method `grid_fs_database='

is there any solution to this?



Looks like the reason is Mongoid 3 had no Grid FS Support at all, so carrierwave-mongoid in the 3.0 branch doesn't support it.


No GridFS Support
GridFS is marketed as a core database feature, when in fact it is not. It is simply a pattern for storing chunked file data as documents in a collection, just like any other document. The implementation of this behaviour is handled in the client drivers, not in the core database itself, which can lead to discrepencies in how this is handled across platforms.

Even if having this behaviour in the client is acceptable, the effects of this on application performance where you are not just storing file data is quite large. Since files are stored as documents, they consume RAM just as any other document in the database would, and can easily cause memory consumption on your server to max out. There are also limitations in chunking the data, such as you do not have the ability to update a file - you must delete the file and replace it with a new one.

Given this, we did not prioritize any work with GridFS at the front, but there is a gem in the pipeline for those who can wait a bit to upgrade. In the meantime you have a few options.

Use the 10gen driver in conjunction with Mongoid 3 and Moped - the namespaces will not collide.
Take advantage of Ara Howard's mongoid-grid_fs gem: mongoid-grid_fs



rmm5t commented Jan 15, 2013

It is no longer necessary to set the grid_fs_connection config setting with the carrierwave-mongoid 0.3.0. It now depends on mongoid-grid_fs, which automatically uses the Mongoid connection.

This was unclear in the README, because the README still referenced the old config mechanism. Sorry about that, but I just remedied that in the README.

@rmm5t rmm5t closed this Jan 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment