otrub
otrub

Reputation: 21

Uninitialized constant Mongoid::Contextual::Queryable::Key

I'm studying Ruby on Rails at Coursera's courses. Have a problem implementing task on my current step within current rspec test. It happened after I implemented foreign_key: "racer.racer_id" These are my classes below and error.

My Racer model class:

class Racer
  include Mongoid::Document

  embeds_one :info, class_name: "RacerInfo", autobuild: true
  has_many :races, class_name: "Entrant", foreign_key: "racer.racer_id", dependent: :nullify, order: :"race.date".desc

  before_create do |racer|
    racer.info.id = racer.id
  end
end

Entrant class:

class Entrant
  include Mongoid::Document
  include Mongoid::Timestamps

  store_in collection: "results"

  field :bib, type: Integer
  field :secs, type: Float
  field :o, as: :overall, type: Placing
  field :gender, type: Placing
  field :group, type: Placing

  embeds_many :results, class_name: "LegResult", after_add: :update_total
  embeds_one :race, class_name: "RaceRef"
  embeds_one :racer, class_name: "RacerInfo"

  default_scope ->{ order(:"event.o".asc) }

  def update_total(result)
    self[:secs] = results.map {|result| result[:secs]}.inject(:+)
  end

  def the_race
    self.race.race
  end
end

RaceRef class:

class RaceRef
  include Mongoid::Document
  field :n, as: :name, type: String
  field :date, type: Date

  embedded_in :entrant, class_name: "Entrant"
  belongs_to :race, foreign_key: "_id"
end

Error:

Failures:

1) Module #3 Summative: Implement Racers / Results Cross-Collection rq02 Racer has a 1:M linked relationship with Entrant with foreign key in Entrant.RacerInfo

Failure/Error: expect(Racer).to have_many(:races).with_dependent(:nullify).ordered_by(:"race.date".desc)

NameError:

uninitialized constant Mongoid::Contextual::Queryable::Key

# ./spec/racer_results_spec.rb:78:in `block (3 levels) in <top (required)>'

# ./spec/racer_results_spec.rb:17:in `block (2 levels) in <top (required)>'

Gemfile:

source 'https://rubygems.org'

gem 'rails', '~> 4.2.11'
gem 'sqlite3', '~> 1.3.13'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

group :development, :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring', '~> 2.0.2'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'mongoid'#, '~> 5.0.0'

group :test do
  gem 'rspec-rails', '~> 3.0'
  gem 'mongoid-rspec'#, '3.0.0'
  gem 'capybara', '~> 3.15.1'
end

gem 'rails-ujs'
gem 'bson', '~> 4.5.0'

Upvotes: 1

Views: 299

Answers (2)

otrub
otrub

Reputation: 21

Wow! After some manipulating with gem versions I've unstall. Here is set of gems which have allowed to pass current rspec and make me happy.

source 'https://rubygems.org'

gem 'rails', '4.2.11.1'
gem 'sqlite3', '1.3.13', group: :development
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc

group :development, :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
end

group :test do
    gem 'rspec-rails', '3.3.0'
    gem 'mongoid-rspec', '3.0.0'
    gem 'capybara', '3.15.1'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'mongoid', '~> 5.4.1'
gem 'bson', '~> 4.5.0'

I should use specific versions of gems 'mongoid', '5.4.1','rspec-rails', '3.3.0' and 'mongoid-rspec', '3.0.0'. After that uninstall other versions to not to use they implicitly in dependencies.

Upvotes: 1

D. SM
D. SM

Reputation: 14480

First, I want to say that the models are very difficult to make sense of. Maybe it's because the course wants to go through the various options that Mongoid supports, but there doesn't seem to be a logical structure that underlies the code. In particular the overloading of various race-related nouns and results:

class Entrant

  store_in collection: "results"

  embeds_many :results, class_name: "LegResult", after_add: :update_total
end

The example given is pretty much spaghetti code.

On to your question, the exception originates in the test file so you'll need to look at what it does (i.e. provide the source to it in your question).

If you are indeed using Mongoid 5 as the gemfile comment suggests, this is another major issue. If you are learning about Mongoid you should be using the current version which is 7. Mongoid 5 is no longer supported. This would require using a newer version of Rails as well (Rails 4 is also not something you should be learning today).

Upvotes: 0

Related Questions