[英]Uninitialized constant Mongoid::Contextual::Queryable::Key
I'm studying Ruby on Rails at Coursera's courses.我正在 Coursera 的课程中学习 Ruby on Rails。 Have a problem implementing task on my current step within current rspec test.
在当前 rspec 测试中的当前步骤中执行任务时遇到问题。 It happened after I implemented
foreign_key: "racer.racer_id"
These are my classes below and error.它发生在我实现了
foreign_key: "racer.racer_id"
这些是我下面的类和错误之后。
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: RaceRef 类:
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'
Wow!哇! After some manipulating with gem versions I've unstall.
在对 gem 版本进行了一些操作后,我已经停止了。 Here is set of gems which have allowed to pass current rspec and make me happy.
这是一组允许通过当前 rspec 并让我高兴的宝石。
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'
.我应该使用特定版本的宝石
'mongoid', '5.4.1'
, 'rspec-rails', '3.3.0'
和'mongoid-rspec', '3.0.0'
。 After that uninstall other versions to not to use they implicitly in dependencies.之后卸载其他版本以不在依赖项中隐式使用它们。
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.
可能是因为课程想要通过 Mongoid 支持的各种选项,但似乎没有代码背后的逻辑结构。 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 (ie 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.如果您确实像 gemfile 评论所暗示的那样使用 Mongoid 5,那么这是另一个主要问题。 If you are learning about Mongoid you should be using the current version which is 7. Mongoid 5 is no longer supported.
如果您正在学习 Mongoid,您应该使用当前版本 7。不再支持 Mongoid 5。 This would require using a newer version of Rails as well (Rails 4 is also not something you should be learning today).
这也需要使用较新版本的 Rails(Rails 4 也不是你今天应该学习的东西)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.