1

我有一个 REST API 路由,它应该限制在显示“模型”对象时包含多少引用的“事物”文档。

主要型号:

class Model
  include Mongoid::Document

  has_many :things
end

参考型号:

class Thing
  include Mongoid::Document

  belongs_to :model
end

模型控制器:

def show
  # this should retrieve the first 30 "Things" that belong to the model we found
  @model = Model.find(params[:id])

  # I attempted this with no luck:
  # @model = Model.find(params[:id]).includes(:things).limit(30)
end

如何检索作为模型一部分的前 N ​​个引用记录?

4

1 回答 1

0

这是解决方案

@things = Thing.where(:model_id => @model.id).limit(30)
于 2012-09-06T05:34:22.447 回答