我正在构建后端系统,正如Iain Hecker的教程:http://iain.nl/backends-in-rails-3-1中所写,我试图用Mongoid将其适应MongoDB.

所以当我需要在backend/resource_helper中编写时.rb

module Backend::ResourceHelper

  def attributes
    resource_class.attribute_names - %w(id created_at updated_at)
  end

end

我得到以下错误:

undefined method `attribute_names' for Backend::User:Class

(我将backend根目录改为"backend/users#index").

class User
  include Mongoid::Document

  devise_for :users

  field :name
  field :address
end

我只需要该用户的一个字段列表:Class,我猜(即[邮箱]、[姓名]、[地址]、…]),但我在寻找方法时伤了头.

推荐答案

Mongoid已经为您提供了对象的属性:

Model.new.attributes

要获取这些属性的名称,请执行以下操作:

Model.fields.keys

Mongodb相关问答推荐

在一个视图中连接两个集合MongoDB;展开有什么作用?

如何对嵌套在另一个数组中的数组中的字段执行查找?

try 从数据库创建、获取数据时失败(pymongo、MongoDB、docker-compose)

获取响应周期中的特定键和值

匹配/筛选/投影对象中数组中数组中的嵌套字段

MongoDB $lookup 查找字段值数组

如何在mongodb中级联删除文档?

替换 MongoDB 中数组中的嵌入文档

使用 Flask-pymongo 扩展通过 _id 在 MongoDB 中搜索文档

在 Mongoose 中清理用户输入

mongodb上不区分大小写的查询

为多个日期范围聚合 $group

是否有适用于 Linux 的 MongoDB GUI 桌面应用程序?

MongoDB - 投影一个并不总是存在的字段

Django admin 和 MongoDB,可能吗?

在 mongodb 中插入当前日期时间

Java + MongoDB:更新文档中的多个字段

缩短 MongoDB 属性名称值得吗?

MongoDB InsertMany 与 BulkWrite

如何使用 Spring 的 MongoTemplate 和 Query 类检索字段子集?