0

Mongoid 2.4.4 Rails 3.2.1 MongoDB 2.0.1

我有 3 个模型:

认证、收藏和内容。</p>

它就像从身份验证到内容到收藏夹的多对多关系。

当我这样查询时:

@favorites = Favorite.any_in(authentication_id: list).includes(:content, :authentication).desc(:collected_at).page params[:page]

@favorites 工作正常

@favorites 的内容也可以正常工作。

但是@favorites 的身份验证只有_id,所有其他字段都为零

当我设置 identity_map_enabled: false 时,它​​已修复。

这是 identity_map 的错误吗?

这是tracert的一些信息,
当启用身份映射时,您可以看到 f.weibo.name 为 nil。

当 identity_map_enabled: true 时如何解决这个问题?

- - - - - - - - - - - 楷模 - - - - - - - - - - - - - - -----

class Authentication
 include Mongoid::Document
 include Mongoid::Timestamps
 include Service::Weibo::Api::Oauth
 cache

 field :_id, :type => Integer
 field :uid, :type => Integer
 field :name, :type => String

 has_many :favorites
end

class Favorite
 include Mongoid::Document
 include Mongoid::Timestamps
 cache
 paginates_per 20

 field :collected_at, :type => Time, :default => Time.now

 belongs_to :authentication
 belongs_to :content

 def weibo
   self.authentication
 end

 def weibo_ids
   self.authentication_ids
 end

end

class Content
 include Mongoid::Document
 include Mongoid::Timestamps
 cache

 field :text, :type => String

 has_many :favorites
end

-------------- 调试代码:------------

    logger.debug "list: #{list}"
   @favorites = Favorite.any_in(authentication_id:
list).includes(:content, :authentication).desc(:collected_at).page
params[:page]
   @favorites.each do |f|
     logger.debug f
     logger.debug "name: #{f.weibo.name}"

    logger.debug "before name: #{@favorites.first.weibo.name}"
    clist =  [764826410]
 #    cf = Favorite.any_in(content_id:
clist).includes(:content, :authentication)
    logger.debug "after query name: #{@favorites.first.weibo.name}"
 #   cf.each {|f| logger.debug "cname: #{f.weibo.name}" }
    logger.debug "after logger name: #{@favorites.first.weibo.name}"

--------- 调试代码结束 ---------

--------- LOG identity_map_enabled: true --------

[0408 12:42:38(3129)DEBUG] list: [1751951491]
 15 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['system.namespaces'].find({})
 16 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(20).sort([[:collected_at, :desc]])
 17 [0408 12:42:38(3129)DEBUG] #<Favorite _id:
4f80c04953cd210b56000003, _type: nil, created_at: 2012-04-07 22:31:37
UTC, updated_at: 2012-04-07 22:31:37 UTC, collected_at
   : 2012-04-07 20:46:37 UTC, authentication_id: 1751951491,
content_id: 3418929751988429>
 18 [0408 12:42:38(3129)DEBUG] name:
 19 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 20 [0408 12:42:38(3129)DEBUG] before name:
 21 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 22 [0408 12:42:38(3129)DEBUG] after query name:
 23 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 24 [0408 12:42:38(3129)DEBUG] after logger name:
 25 [0408 12:42:38(3129)DEBUG] #<Favorite _id:
4f80c04953cd210b56000005, _type: nil, created_at: 2012-04-07 22:31:37
UTC, updated_at: 2012-04-07 22:31:37 UTC, collected_at
   : 2012-04-07 20:46:37 UTC, authentication_id: 1751951491,
content_id: 3418840442279122>
 26 [0408 12:42:38(3129)DEBUG] name:
 27 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 28 [0408 12:42:38(3129)DEBUG] before name:
 29 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 30 [0408 12:42:38(3129)DEBUG] after query name:
 31 [0408 12:42:38(3129)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 32 [0408 12:42:38(3129)DEBUG] after logger name:
 33 [0408 12:42:38(3129)DEBUG] #<Favorite _id:
4f80c04953cd210b56000007, _type: nil, created_at: 2012-04-07 22:31:37
UTC, updated_at: 2012-04-07 22:31:37 UTC, collected_at
   : 2012-04-07 20:46:37 UTC, authentication_id: 1751951491,
content_id: 3368827987931269>

--------- LOG identity_map_enabled: false --------------------

17 [0408 12:37:53(3092)DEBUG] list: [1751951491]
 18 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['system.namespaces'].find({})
 19 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(20).sort([[:collected_at, :desc]])
 20 [0408 12:37:53(3092)DEBUG] #<Favorite _id:
4f80c04953cd210b56000003, _type: nil, created_at: 2012-04-07 22:31:37
UTC, updated_at: 2012-04-07 22:31:37 UTC, collected_at
   : 2012-04-07 20:46:37 UTC, authentication_id: 1751951491,
content_id: 3418929751988429>
 21 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 22 [0408 12:37:53(3092)DEBUG] name: magazine
 23 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 24 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 25 [0408 12:37:53(3092)DEBUG] before name: magazine
 26 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 27 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 28 [0408 12:37:53(3092)DEBUG] after query name: magazine
 29 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 30 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 31 [0408 12:37:53(3092)DEBUG] after logger name: magazine
 32 [0408 12:37:53(3092)DEBUG] #<Favorite _id:
4f80c04953cd210b56000005, _type: nil, created_at: 2012-04-07 22:31:37
UTC, updated_at: 2012-04-07 22:31:37 UTC, collected_at
   : 2012-04-07 20:46:37 UTC, authentication_id: 1751951491,
content_id: 3418840442279122>
 33 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 34 [0408 12:37:53(3092)DEBUG] name: magazine
 35 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 36 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 37 [0408 12:37:53(3092)DEBUG] before name: magazine
 38 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 39 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 40 [0408 12:37:53(3092)DEBUG] after query name: magazine
 41 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['favorites'].find({:authentication_id=>{"$in"=>[1751951491]}}).limit(-1).sort([[:collected_at, :desc]])
 42 [0408 12:37:53(3092)DEBUG] MONGODB
weibofav_development['authentications'].find({:_id=>1751951491}).limit(-1).sort([[:_id, :asc]])
 43 [0408 12:37:53(3092)DEBUG] after logger name: magazine
4

1 回答 1

0

Rails 3.2.1、Mongoid 2.4.8、MongoDB 2.0.4

我复制了你的代码,除了我没有 Service::Weibo::Api::Oauth 也没有工作微博帐户来尝试测试这方面。但是,我有以下结果和说明,希望对您有所帮助。

authentication_id 和 content_id 是 Mongoid 生成的参考关系,其类型为 BSON::ObjectId,具有十六进制标识符字符串。在您的调试代码和日志中,显示了(不正确的)fixnum,因此您可能需要检查它并确保您的查询是正确的。

以下对我有用,结果符合预期,除了 _type 之外没有任何 nil 字段,无需修改 identity_map。您可能应该怀疑没有使用正确的 BSON::ObjectId 或您的微博代码。希望这会有所帮助。

-加里

list = [1751951491]
Rails.logger.debug "list.first: #{list.first}"
auth = Authentication.create(uid: list.first, name: 'authenication name')
content = Content.create(text: 'content text')
favorite = Favorite.new
auth.favorites << favorite
Rails.logger.debug "auth.favorites:#{auth.favorites.inspect}"
assert_equal(1, auth.favorites.length)
content.favorites << favorite
Rails.logger.debug "content.favorites:#{content.favorites.inspect}"
assert_equal(1, content.favorites.length)
auth_list = [auth._id.to_s]
Rails.logger.debug "auth_list:#{auth_list.inspect}"
@favorites = Favorite.any_in(authentication_id: auth_list).to_a
assert_equal(1, @favorites.length)
Rails.logger.debug "@favorites:#{@favorites.inspect}"
@favorites = Favorite.any_in(authentication_id: auth_list).includes(:content, :authentication).desc(:collected_at).to_a
assert_equal(1, @favorites.length)
Rails.logger.debug "@favorites:#{@favorites.inspect}"
@favorites.each do |f|
  Rails.logger.debug f.inspect
  Rails.logger.debug f.authentication.inspect
  Rails.logger.debug f.content.inspect
end
于 2012-04-13T19:44:08.677 回答