加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – mongoid模型的密钥不起作用

发布时间:2020-12-17 03:38:57 所属栏目:百科 来源:网络整理
导读:我安装了gem mongoid到RoR应用程序,但收到错误“未定义的方法`键?”为nil:NilClass“,同时为键功能指定属性. 所以我有这个模型: class Author include Mongoid::Document field :name key :name end 并默认脚手架控制器和运行此url localhost:3000 / aut
我安装了gem mongoid到RoR应用程序,但收到错误“未定义的方法`键?”为nil:NilClass“,同时为键功能指定属性.

所以我有这个模型:

class Author
      include Mongoid::Document
      field :name
      key :name
    end

并默认脚手架控制器和运行此url localhost:3000 / authors,收到此错误

NoMethodError in AuthorsController#index

undefined method `key?' for nil:NilClass
Rails.root: C:/Users/Jeremy/RubymineProjects/university

宝石文件:

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'

我从这里使用了http://railscasts.com/episodes/238-mongoid?view=asciicast指南,做了两次相同的事情.

解决方法

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

文件:http://mongoid.org/en/mongoid/docs/upgrading.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读