mongoid 标准结果未填满所有字段
mongoid criteria result doesn't fill all field
我是 Rails 和 MongoDB 以及 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:
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
我正在使用 MongoID 查询我的 mongodb 数据库,如下所示:
myuser = User.where(fbid: params[:fbid]).first
render :json => myuser.writesajson
然而,结果是所有字段都是"null"
如果我这样打印条件结果,
render :json => myuser
它打印所有 _id
、authData
和 bcryptPassword
字段,但是该字段的其余部分具有 null
值,
这是我从我的应用程序中的 MongoDB 数据库中获得的信息。如果我从 MongoHub 查询,所有空值都将被填充
{
"_id": {
"$oid": "56d2872f00af597fa584e367"
},
"authData": {
"facebook": {
"access_token": "yEf8cZCs9uTkrOq0ZCHJJtgPFxPAig9yhW6DhBCLuJqPdMZBLPu",
"expiration_date": "2016-04-17T13:52:12.000Z",
"id": "9192631770"
}
},
"bcryptPassword": "amUW3JWI51GxM1VilA",
"competitorFbid": null,
"createdAt": null,
"created_at": null,
"facebookname": null,
"fbid": null,
"objectId": "nLurZcAfBe",
"runCount": 2446,
"sessionToken": "0SwPDVDu",
"updatedAt": null,
"updated_at": null,
"username": "XgcWo4iUCK"
}
我在没有任何光线的情况下调试了一整天,任何帮助将不胜感激...
编辑:
添加响应
{"_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]"}
数据库中的一个字段使用field
方法声明:
field :fbid, type: String
这也定义了 fbid
和 fbid=
方法来处理 fbid
属性 .
使用 attr_accessor
方法声明具有关联访问器和修改器方法的实例变量:
attr_accessor :fbid
这还将添加 fbid
和 fbid=
方法来处理基础 实例变量 .
它们不是一回事。 Mongoid 只知道 field
s,这些是它将在数据库中使用的东西,因此您的查询有效; field
还为您的字段定义访问器和修改器方法。
但是你有一个 attr_accessor
调用 在你的 field
调用之后 所以 field
创建的方法(例如 fbid
和 fbid=
) 被 attr_accessor
创建的覆盖。结果是您的所有属性似乎都是 nil
.
解决方案是从您的 class 中删除 attr_accessor
呼叫。您只需要 field
个电话。
我是 Rails 和 MongoDB 以及 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:
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
我正在使用 MongoID 查询我的 mongodb 数据库,如下所示:
myuser = User.where(fbid: params[:fbid]).first
render :json => myuser.writesajson
然而,结果是所有字段都是"null"
如果我这样打印条件结果,
render :json => myuser
它打印所有 _id
、authData
和 bcryptPassword
字段,但是该字段的其余部分具有 null
值,
这是我从我的应用程序中的 MongoDB 数据库中获得的信息。如果我从 MongoHub 查询,所有空值都将被填充
{
"_id": {
"$oid": "56d2872f00af597fa584e367"
},
"authData": {
"facebook": {
"access_token": "yEf8cZCs9uTkrOq0ZCHJJtgPFxPAig9yhW6DhBCLuJqPdMZBLPu",
"expiration_date": "2016-04-17T13:52:12.000Z",
"id": "9192631770"
}
},
"bcryptPassword": "amUW3JWI51GxM1VilA",
"competitorFbid": null,
"createdAt": null,
"created_at": null,
"facebookname": null,
"fbid": null,
"objectId": "nLurZcAfBe",
"runCount": 2446,
"sessionToken": "0SwPDVDu",
"updatedAt": null,
"updated_at": null,
"username": "XgcWo4iUCK"
}
我在没有任何光线的情况下调试了一整天,任何帮助将不胜感激...
编辑: 添加响应
{"_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]"}
数据库中的一个字段使用field
方法声明:
field :fbid, type: String
这也定义了 fbid
和 fbid=
方法来处理 fbid
属性 .
使用 attr_accessor
方法声明具有关联访问器和修改器方法的实例变量:
attr_accessor :fbid
这还将添加 fbid
和 fbid=
方法来处理基础 实例变量 .
它们不是一回事。 Mongoid 只知道 field
s,这些是它将在数据库中使用的东西,因此您的查询有效; field
还为您的字段定义访问器和修改器方法。
但是你有一个 attr_accessor
调用 在你的 field
调用之后 所以 field
创建的方法(例如 fbid
和 fbid=
) 被 attr_accessor
创建的覆盖。结果是您的所有属性似乎都是 nil
.
解决方案是从您的 class 中删除 attr_accessor
呼叫。您只需要 field
个电话。