Mongoid 包括不能正常工作
Mongoid includes not working properly
美好的一天!
我正在尝试为 mongoDB 加载模型:
User.includes(:party).first
在 mongoid 的调试模式下,我看到下一个请求:
D, [2015-12-19T13:53:19.354800 #3847] DEBUG -- : MONGODB | STARTED | {"find"=>"users", "filter"=>{}}
D, [2015-12-19T13:53:19.355403 #3847] DEBUG -- : MONGODB | SUCCEEDED | 0.000451s
D, [2015-12-19T13:53:19.366237 #3847] DEBUG -- : MONGODB | STARTED | {"find"=>"parties", "filter"=>{"user_id"=>{"$in"=>[nil]}}}
D, [2015-12-19T13:53:19.366626 #3847] DEBUG -- : MONGODB | SUCCEEDED | 0.000307s
如您所见,使用此参数执行的第二个请求:
{"find"=>"parties", "filter"=>{"user_id"=>{"$in"=>[nil]}}}
我不明白为什么,这是我的模型:
class User
include Mongoid::Document
include Mongoid::Timestamps
field :nickname, type: String
field :email, type: String
has_one :party
has_many :added_songs, class_name: 'Playlist::Song'
has_many :chat_messages, class_name: 'Chat::Message'
end
class Party
include Mongoid::Document
include Mongoid::Timestamps
field :active, type: Boolean, default: true
field :title, type: String
belongs_to :user
has_one :chat, class_name: 'Party::Chat'
has_one :playlist, class_name: 'Party::Playlist'
end
有趣的是,如果我这样做:
Party.includes(:user).first
它工作正常。感谢您的帮助!
您不一定指定 "it works" 的含义,但我假设您的意思是它实际上 returns 来自数据库的文档。据我所知,您的急切负载正在按预期工作。
您指定 User
有一个 Party
。这意味着 user_id
存储在 Party
中,而 party_id
是 而不是 存储在 User
.
中
所以当您 运行 这个查询时:
User.includes(:party).first
它将专门查找没有特定 User
:
的 Party
个对象
"find"=>"parties", "filter"=>{"user_id"=>{"$in"=>[nil]}}
所以如果你反过来:
Party.includes(:user).first
它不能够做到这一点:
"find"=>"users", "filter"=>{"party_id"=>{"$in"=>[nil]}}
因为 User
集合中没有 party_id
存储。
所以如果你想 运行 User.includes(:party).first
你应该先尝试找到一个特定的 User
,比如:
User.where(:_id => "one of your users").includes(:party).first
美好的一天!
我正在尝试为 mongoDB 加载模型:
User.includes(:party).first
在 mongoid 的调试模式下,我看到下一个请求:
D, [2015-12-19T13:53:19.354800 #3847] DEBUG -- : MONGODB | STARTED | {"find"=>"users", "filter"=>{}}
D, [2015-12-19T13:53:19.355403 #3847] DEBUG -- : MONGODB | SUCCEEDED | 0.000451s
D, [2015-12-19T13:53:19.366237 #3847] DEBUG -- : MONGODB | STARTED | {"find"=>"parties", "filter"=>{"user_id"=>{"$in"=>[nil]}}}
D, [2015-12-19T13:53:19.366626 #3847] DEBUG -- : MONGODB | SUCCEEDED | 0.000307s
如您所见,使用此参数执行的第二个请求:
{"find"=>"parties", "filter"=>{"user_id"=>{"$in"=>[nil]}}}
我不明白为什么,这是我的模型:
class User
include Mongoid::Document
include Mongoid::Timestamps
field :nickname, type: String
field :email, type: String
has_one :party
has_many :added_songs, class_name: 'Playlist::Song'
has_many :chat_messages, class_name: 'Chat::Message'
end
class Party
include Mongoid::Document
include Mongoid::Timestamps
field :active, type: Boolean, default: true
field :title, type: String
belongs_to :user
has_one :chat, class_name: 'Party::Chat'
has_one :playlist, class_name: 'Party::Playlist'
end
有趣的是,如果我这样做:
Party.includes(:user).first
它工作正常。感谢您的帮助!
您不一定指定 "it works" 的含义,但我假设您的意思是它实际上 returns 来自数据库的文档。据我所知,您的急切负载正在按预期工作。
您指定 User
有一个 Party
。这意味着 user_id
存储在 Party
中,而 party_id
是 而不是 存储在 User
.
所以当您 运行 这个查询时:
User.includes(:party).first
它将专门查找没有特定 User
:
Party
个对象
"find"=>"parties", "filter"=>{"user_id"=>{"$in"=>[nil]}}
所以如果你反过来:
Party.includes(:user).first
它不能够做到这一点:
"find"=>"users", "filter"=>{"party_id"=>{"$in"=>[nil]}}
因为 User
集合中没有 party_id
存储。
所以如果你想 运行 User.includes(:party).first
你应该先尝试找到一个特定的 User
,比如:
User.where(:_id => "one of your users").includes(:party).first