/ / Ruby on Rails + MongoDB und MongoID - Ruby-on-Rails-3, Mongodb, Mongoid, Mongoid3

Ruby on Rails + MongoDB und MongoID - Rubin-auf-Schienen-3, Mongodb, Mongoid, Mongoid3

Ich habe mit MongoDB einen Beispiel-DB mit Daten erstelltaus einer JSON-Datei auf der Mongodbs-Website Ich habe es mit dem folgenden Befehl importiert. mongoimport --db test --collection zips --file zips.json Die Daten werden verarbeitet und wenn ich versuche, in der Konsole nach Daten zu suchen, werden sie angezeigt, soweit so gut.

Mein Problem ist, wenn ich versuche, die DB mit meiner Rails-App zu verwenden. Ich habe eine Klasse, eine Stadt, erstellt. Der Code sieht folgendermaßen aus.

    class City
include Mongoid::Document
field :c, as: :city, type: String
field :l, as: :loc, type: Array
field :p, as: :population, type: Integer
field :s, as: :state, type: String
field :_id, type: Integer
end

Und meine mongoid.yml-Datei sieht so aus

    development:
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: exjobb
# Provides the hosts the default session can connect to. Must be an array
# of host:port pairs. (required)
hosts:
- localhost:27017
options:

options:

test:
sessions:
default:
database: exjobb
hosts:
- localhost:27017
options:
consistency: :strong
# In the test environment we lower the retries and retry interval to
# low amounts for fast failures.
max_retries: 1
retry_interval: 0

Activerecord ist deaktiviert. Wenn ich Rails Console starte und City.where versuche ... Ich erhalte die folgende Ausgabe.

    City.where(city: "Acmar")
=> #<Mongoid::Criteria
selector: {"c"=>"Acmar"}
options:  {}
class:    City
embedded: false>

Und wenn ich City.first versuche wird ein Fehler geworfen

    NoMethodError: undefined method `to_sym" for nil:NilClass
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:409:in `__evaluate__"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:357:in `__database_name__"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:198:in `database_name"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:429:in `current_database_name"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:228:in `mongo_session"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:171:in `collection"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual/mongo.rb:256:in `initialize"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:48:in `new"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:48:in `create_context"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:31:in `context"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:21:in `rescue in first"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:19:in `first"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/finders.rb:117:in `first"
from (irb):1
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start"
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/railties-    3.2.11/lib/rails/commands.rb:41:in `<top (required)>"
from script/rails:6:in `require"

Ich habe meinen Code hochgeladen HierBitte klone und versuche mir zu helfen, ich stecke fest. Wie Sie sehen, ist meine Datenbank nicht leer. http://i.stack.imgur.com/Bho2H.png

Antworten:

0 für die Antwort № 1

Mongoid (mit ähnlichen Zuordnungskonventionen wie ActiveRecord) pluralisiert den Klassennamen, um den Sammlungsnamen zu erhalten. Anscheinend haben Sie eine Sammlung namens city Aber Mongoid hat Ihre Klasse einer Sammlung mit dem Namen zugeordnet cities.

Sie können den Sammlungsnamen überschreiben:

class City
store_in collection: "city"
end

Oder importieren Sie Ihre Daten noch besser in eine Städtesammlung.