I am new to Rails and MongoDB as well as MongoID..
class User
include Mongoid::Document
include Mongoid::Timestamps
field :fbid, type: String
field :facebookname, type: String
field :competitorFbid, type: String
field :createdAt, type: DateTime
field :updatedAt, type: DateTime
# building constructor the rails way: http://stackoverflow.com/a/3214293/474330
def initialize(options = {})
@fbid = options[:fbid]
@facebookname = options[:facebookname]
@competitorFbid = options[:competitorFbid]
end
def writeasjson
hash = { :fbid => @fbid,
:facebookname => @facebookname,
:competitorFbid => @competitorFbid,
:createdAt => @createdAt,
:updatedAt => @updatedAt
}
hash.to_json
end
attr_accessor :fbid, :facebookname, :competitorFbid, :createdAt, :updatedAt
end
I am using MongoID to query my mongodb database like this:
myuser = User.where(fbid: params[:fbid]).first
render :json => myuser.writesajson
However, the result is all the fields are "null"
If I print the criteria result like this,
render :json => myuser
it prints all the _id
, authData
and bcryptPassword
field, however the rest of the field have null
value,
Here is what I got from the MongoDB database in my app. If I query from MongoHub, all the null values will be filled
{
"_id": {
"$oid": "56d2872f00af597fa584e367"
},
"authData": {
"facebook": {
"access_token": "yEf8cZCs9uTkrOq0ZCHJJtgPFxPAig9yhW6DhBCLuJqPdMZBLPu",
"expiration_date": "2016-04-17T13:52:12.000Z",
"id": "9192631770"
}
},
"bcryptPassword": "$2a$10$9mUW3JWI51GxM1VilA",
"competitorFbid": null,
"createdAt": null,
"created_at": null,
"facebookname": null,
"fbid": null,
"objectId": "nLurZcAfBe",
"runCount": 2446,
"sessionToken": "0SwPDVDu",
"updatedAt": null,
"updated_at": null,
"username": "XgcWo4iUCK"
}
I have been debugging the whole day without any light, any help will be greatly appreciated...
EDIT: adding the response
{"_id":{"$oid":"56d2872f00af597fa584e366"},"authData":{"facebook":{"access_token":"[ACCESS_TOKEN_REMOVED]","expiration_date":"2015-12-19T14:17:25.000Z","id":"[ID_REMOVED]"}},"bcryptPassword":"[PASSWORD_REMOVED]","competitorFbid":null,"createdAt":null,"created_at":null,"facebookname":null,"fbid":null,"objectId":"H5cEMtUzMo","runCount":790,"sessionToken":"[SESSION_TOKEN_REMOVED]","updatedAt":null,"updated_at":null,"username":"[USERNAME_REMOVED]"}