groc426
groc426

Reputation: 319

Rails - rabl and gon gem return collection of joined tables

This is my first time using the rabl and gon gem. I'm simply attempting to output the results of a query using rabl. Unfortunately, I'm not getting the proper objects back.

My controller:

@test = Category.joins('LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = 86').group_by { |category| category.cat_ds }

My rabl template:

collection @test
attributes :cat_code, :cat_ds

The result I hope to get:

{
    "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
                }
            ]
        }
    ]
}

I've removed most of the results above.

The results I'm getting:

{

        "[#<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\">]": {}
            }
        ]
    }

So for every group like Revenue and Expense its returning one record, the same record. Why would it simply not parse the instance variable I gave it. What else is it doing to the data before presenting it.

Also worth to note, if i skip the rabl template and use the variable from gon, it displays perfectly. So it must be something to do with my rabl template?

Edit I want to see if the below worked, but I get the following error: 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

Upvotes: 1

Views: 424

Answers (1)

Ismael
Ismael

Reputation: 16720

Try this in your controller. Rabl is used to render, so if you want to use it to pass to gon I think you have to render it manually since you will render HTML for the request.

@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, 'test/show')

Upvotes: 1

Related Questions