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