我正在构建后端系统,如Iain Hecker的教程中所述:http://iain.nl/backends-in-rails-3-1,我尝试使用Mongoid将其调整为MongoDB.
所以当我需要写backend/resourse_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
(我将后端植根到"后端/用户#index").后端::用户继承自用户:
class User include Mongoid::Document devise_for :users field :name field :address end
我只需要该用户的字段列表:类,我猜(即["电子邮件","名称","地址",...]),但我试图找到方法.
Mongoid已经为您提供了对象的属性:
Model.new.attributes
要获取这些属性的名称:
Model.fields.keys
使用内置方法:
Model.attribute_names # => ["_id", "created_at", "updated_at", "name", "address"]