I installed gem mongoid to RoR application, but received error "undefined method `key?' for nil:NilClass" while assign attribute to key function.
So i have this model:
class Author
include Mongoid::Document
field :name
key :name
end
And default scaffold controller and running this url localhost:3000/authors , receive this error
NoMethodError in AuthorsController#index
undefined method `key?' for nil:NilClass
Rails.root: C:/Users/Jeremy/RubymineProjects/university
Gem file:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem "mongoid", "~> 3.0.0"
gem "bson_ext", "~> 1.2"
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
I used guide from here http://railscasts.com/episodes/238-mongoid?view=asciicast, did everything the same two times.
Model.identity and Model.key have been removed. For custom ids, users must now override the _id field. When the default value is a proc, the default is applied after all other attributes are set.
class Band
include Mongoid::Document
field :_id, type: String, default: ->{ name } # which was `key :name` before v3
end
Docs: http://mongoid.org/en/mongoid/docs/upgrading.html
Mongoid has changed a lot with version 3. (the asciicast wants gem 'mongoid', '2.0.0.beta.19'
...)
Check mongoid documentation at http://mongoid.org/en/mongoid/index.html
In the end, it seems that custom and composited key fields are not supported in Mongoid 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With