1
這是我第一次使用拉寶和坤寶石。我只是試圖用rabl輸出查詢的結果。不幸的是,我沒有找回適當的對象。Rails - 拉寶和坤寶石迴歸集合表
我的控制器:
@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\">]": {}
}
]
}
所以對於每個組像營收和費用及其返回一個記錄,相同的記錄。爲什麼它只是不解析我給它的實例變量。在呈現數據之前它還對數據做了什麼?
同樣值得注意的是,如果我跳過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
謝謝你的解釋,這有幫助。另一個問題是,我得到以下錯誤:'在註冊的([])視圖路徑中找不到Rabl模板'收入/索引'!「任何想法?我已經更新了我的問題。 – groc426