コントローラに次のようなものがあるとしましょう:
@org = Org.includes(programs: :patient_counts]).find(params[:id])
respond_with(@org)
そして今、私はこれをJBuilderに渡します。
json.program @org.programs do |program|
json.(program, :name)
# more code to also return some info from patien_counts table too
end
したがって、200個のプログラムがあり、1対1の関係で200人のpatient_countsもある場合、返されるJSONには200個のオブジェクトが含まれます。しかし、私の場合、私はそれらの特定の量だけが欲しいです。たとえば、patient_countsテーブルにSalaryとBonusという2つのフィールドがあり、JSONで15個のオブジェクトを返したいとします。これらの200個のオブジェクトすべてではなく、Salary+Bonusが最も高いのは15個だけです。
このシナリオのようなロジックと計算の場合、どうすればよいですか?
編集:モデルに関する情報:
program.rb :
name:string
has_many: patient_conuts
patient_count.rb:
belongs_to: program
program_id # from the program above
total_amount: integer