rabl と gon の gem を使用するのはこれが初めてです。rabl を使用してクエリの結果を出力しようとしているだけです。残念ながら、適切なオブジェクトを取得できません。
私のコントローラー:
@test = Category.joins('LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = 86').group_by { |category| category.cat_ds }
私のrablテンプレート:
collection @test
attributes :cat_code, :cat_ds
私が得たい結果:
{
"REVENUE": [
{
"cat_code": 10001,
"cat_ds": "REVENUE",
"id": 1,
"incomes": [
{
"category_id": "1.0",
"chart_id": 1,
"created_at": "2013-01-15T16:43:52Z",
"dept_id": 4,
"feb": "11.0",
"id": 1,
"jan": "12.12",
"note": "Testing",
"updated_at": "2013-01-15T16:43:52Z",
"year_id": 1,
"total_cost": 23
}
]
}
],
"EXPENSE": [
{
"cat_code": 10002,
"cat_ds": "EXPENSE",
"id": 2,
"incomes": [
{
"category_id": "2.0",
"chart_id": 2,
"created_at": "2013-01-15T16:43:52Z",
"dept_id": 86,
"feb": "45.0",
"id": 3,
"jan": "60.0",
"note": "Two",
"updated_at": "2013-01-15T16:43:52Z",
"year_id": 1,
"total_cost": 105
},
{
"category_id": "2.0",
"chart_id": 2,
"created_at": "2013-01-15T16:43:52Z",
"dept_id": 86,
"feb": "45.55",
"id": 5,
"jan": "3454.0",
"note": "One",
"updated_at": "2013-01-15T16:43:52Z",
"year_id": 1,
"total_cost": 3499
}
]
}
]
}
上記の結果のほとんどを削除しました。
私が得ている結果:
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": [
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
},
{
"[#<Category id: 1, cat_code: 10001, cat_ds: \"INPATIENT REVENUE\">]": {}
}
]
}
したがって、Revenue や Expense などのすべてのグループに対して、同じレコードの 1 つのレコードが返されます。私が与えたインスタンス変数を単純に解析しないのはなぜですか。データを提示する前に、データに対して他に何をしていますか。
また、rabl テンプレートをスキップして gon の変数を使用すると、完全に表示されます。それで、それは私のrablテンプレートと関係があるに違いありませんか?
編集
以下が機能するかどうかを確認したいのですが、次のエラーが発生します。
Cannot find rabl template 'income/index' within registered ([]) view paths!
def index
@test_obj = Category.joins('LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = 86').group_by { |category| category.cat_ds }
@test = Rabl::Renderer.json(@test_obj, 'income/index')
gon.rabl "app/views/incomes/index.rabl", as: "test"
end