Re: [MongoMapper] Doing an "all" query on a model results in NoMethodError: undefined method `collection' for nil:NilClass

73 views
Skip to first unread message

Kevin Lawver

unread,
Mar 28, 2013, 11:16:21 AM3/28/13
to mongo...@googlegroups.com
set_database_name $DB_NAME in Parameter should be set_database_name $_DB_NAME, if the second example works and Parameter doesn't.

Give that a shot and let us know how it goes!

On Mar 28, 2013, at 10:34 AM, mari...@gmail.com wrote:

Hi there,

this is my first message on a Google group so I hope I'm doing it right.

I'm building my first web application with Ruby, Sinatra and MongoMapper.
So far it's been going good, but now I've run into a very strange error when executing the following line:

    @p = Parameter.all

With Parameter being this class:

    class Parameter
     
    include MongoMapper::Document
      plugin MongoMapper::Plugins::IdentityMap
 
      connection Mongo::Connection.new($_DB_SERVER_CNC)
      set_database_name $DB_NAME
     
      key :name,          String
      key :description,   String 
      key :db,            String
      key :db_table,      String
      key :db_field,      String
      #key :compare,       Array
      #many :parameters, :in => :compare
      key :created_by,    String, :default => 'id833541'
     
      timestamps!
   
    end



The error here is:

NoMethodError: undefined method `collection' for nil:NilClass

And it happens in mongo_mapper-0.12.0/lib/mongo_mapper/plugins/persistence.rb

  1. def collection
  2. assert_supported
  1. database.collection(collection_name) # ******HERE*******
  1. end

The reason why I really don't understand how this could happen is that it works with another class that looks about the same:

  class SettingCategory
   
    include MongoMapper::Document
      plugin MongoMapper::Plugins::IdentityMap
   
      connection Mongo::Connection.new($_DB_SERVER_CNC)
      set_database_name $_DB_NAME
     
      key :name
      key :description
      key :created_by,    String,   :default => 'id833542'
     
      timestamps!
     
      many :settings
   
    end

@sc = SettingCategory.all # works fine



I know I might not be giving a lot of info, including more on my application's structure, but I did my best to boil it down to the basics so you don't get extra clutter here. If you need anything else just let me know...

Thanks to anyone who could shed more light on the situation.

mari...@gmail.com

unread,
Mar 28, 2013, 11:23:03 AM3/28/13
to mongo...@googlegroups.com
Indeed that was the problem.
Funny thing is we've spent hours looking over this detail with 2 people.
Guess that is what you get with uncompiled languages huh?

Thanks for helping out!

Op donderdag 28 maart 2013 16:16:21 UTC+1 schreef Kevin Lawver het volgende:
Reply all
Reply to author
Forward
0 new messages