1
私は4つのテーブルを持って、効率的な埋め込みidsのセットが見つかりましたか?
Shops
Sales
Batches
Products
と私はリコールの場合にバッチ番号に基づいて店頭で販売されているすべての製品を抽出したいと思います(同じタイプの複数の製品の項目がに属することができます同じバッチ)。私の不完全なコードは次のようになります
...
# GET /shop/1/batches
# GET /shop/1/batches.xml
def batches
# Obtain unique batches by searching all sales transactions, looking for unique
# batch IDs.
@sales = Sales.find_all_by_shop_id(params[:id], :group => :batch_id)
# I want to avoid doing an iteration over sales.
# I'd now like to do something like...
# @batches = Batches.find_all_by_id(@sales.batch_id, :include => [:product])
# ...to find the set of batches ^^^^^^^^^^^^^^^
#
#
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @batches.to_xml(:include => [:product], :except => [:created_at, :updated_at]) }
end
end
私は@salesを反復処理し、製品の詳細情報を取得するために、それぞれからのproduct_idを抽出し、それをデータベースに多数のアクセスにつながる、と私でしたRuby/Railsがより良い方法を持っていると考えるのを助けることはできません。
ありがとう、それは情報を返します。私はバッチタイプのアイテムのセットを望んでいたと思いますが、標準ビューを使用することができましたが、必要に応じてカスタムビューを作成できます。 – Snips
@_batches = @ _sales.map {|販売| sale.batch} –
ああ、ありがとう:-) – Snips