1

我想用 mongoid 查询,我有以下模型

class Score
   include Mongoid::Document
   field :value, :type => Integer
   belongs_to :user
end


class User
   include Mongoid::Document
   field :name, :type => String
   field :age, :type => Integer
   has_many :scores

我想与他们的用户一起查询集合中的所有分数。但是用户对象中应该只有“名称”字段

我会是这样的

   Score.find.all.includes(:user).only(:name)

请说出正确的语法

4

1 回答 1

1

Scorereferences User,因此不可能在一个查询中同时检索它们,因为 MongoDB 中没有连接。

非规范化并在其中包含用户名Score或执行多个查询。

于 2012-06-13T19:55:16.733 回答