gpt4 book ai didi

ruby-on-rails - mongoid 模型的 key 不起作用

转载 作者:可可西里 更新时间:2023-11-01 09:10:20 27 4
gpt4 key购买 nike

我将 gem mongoid 安装到 RoR 应用程序,但收到错误消息“未定义的方法 `key?'对于 nil:NilClass",同时将属性分配给键函数。

所以我有这个模型:

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

默认脚手架 Controller 并运行这个 url localhost:3000/authors ,收到这个错误

NoMethodError in AuthorsController#index

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

gem 文件:

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

关于ruby-on-rails - mongoid 模型的 key 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com