0
votes

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]"}
1

1 Answers

1
votes

A field in the database is declared using the field method:

field :fbid, type: String

This also defines fbid and fbid= methods to work with the fbid attribute.

An instance variable with associated accessor and mutator methods is declared using the attr_accessor method:

attr_accessor :fbid

This will also add fbid and fbid= methods to work with the underlying instance variable.

They're not the same thing. Mongoid only knows about fields, those are the things that it will work with in the database so your query works; field also defines accessor and mutator methods for your fields.

But you have an attr_accessor call after your field calls so the methods that field creates (such as fbid and fbid=) are overwritten by those created by attr_accessor. The result is that all your attributes appear to be nil.

The solution is to drop the attr_accessor call from your class. You only need the field calls.