我有一个REST API路由应该限制引用的数量" Thing"显示"模型"宾语。
主要型号:
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个引用记录?
答案 0 :(得分:0)
这是解决方案
@things = Thing.where(:model_id => @model.id).limit(30)