Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key for mongoid model doesn't work

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.

like image 941
Serghei Avatar asked Oct 22 '12 18:10

Serghei


Video Answer


2 Answers

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

like image 84
ted Avatar answered Oct 01 '22 10:10

ted


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

like image 37
rewritten Avatar answered Oct 01 '22 10:10

rewritten