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 に答える