I'm building backend system, as written in Iain Hecker's tutorial: http://iain.nl/backends-in-rails-3-1 and I try to adapt it to MongoDB with Mongoid.
So when I need to write in backend/resourse_helper.rb
module Backend::ResourceHelper
def attributes
resource_class.attribute_names - %w(id created_at updated_at)
end
end
I get the following error:
undefined method `attribute_names' for Backend::User:Class
(I rooted backend to "backend/users#index"). Backend::User inherits from User:
class User
include Mongoid::Document
devise_for :users
field :name
field :address
end
I just need a list of fields for that User:Class, as I guess (i.e. ["email", "name", "address", ...]), but I broke my head trying to find how.
You can use $getField to retrieve the value of fields with names that contain periods ( . ) or start with dollar signs ( $ ).
Field names are strings. Documents have the following restrictions on field names: The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.
In MongoDB, there is no concept of columns since MongoDB is schema-less and does not contain tables. It contains the concept of collections and a collection has different types of documents to store items. db. yourCollectionName.
Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.
Mongoid already provides you the attributes for an object:
Model.new.attributes
To get the names for these attributes:
Model.fields.keys
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With