Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format of source_data #7

Closed
cmcguff opened this issue Apr 23, 2014 · 2 comments
Closed

Format of source_data #7

cmcguff opened this issue Apr 23, 2014 · 2 comments

Comments

@cmcguff
Copy link

cmcguff commented Apr 23, 2014

I'm using the following to construct

data = ActiveRecord::Base.connection.execute("SELECT users.name as user_name, leave_types.name as leave_type_name, leave_balances.balance from leave_balances join users on leave_balances.user_id = users.id join leave_types on leave_balances.leave_type_id = leave_types.id")

grid = PivotTable::Grid.new do |g|
    g.source_data  = data
    g.column_name  = :leave_type_name
    g.row_name     = :user_name
    g.value_name   = :balance
end

This gives me a result like:

[{"user_name"=>"Manager", "leave_type_name"=>"Anltjsjsjs", "balance"=>1.0, 0=>"Manager", 1=>"Anltjsjsjs", 2=>1.0}, {"user_name"=>"Employee",.....

Which fails when I build the grid with errors like:
undefined method `user_name' for #Hash:0x00000103ad0b98

it looks like this should work with an array of hashes, do I need to reformat or select differently somehow?

@cmcguff
Copy link
Author

cmcguff commented Apr 23, 2014

Actually, this may be better:

data = LeaveBalance.includes([:user, :leave_type])
grid = PivotTable::Grid.new do |g|
    g.source_data  = data
    g.column_name  = :leave_type
    g.row_name     = :user
    g.value_name   = :balance
end

I'm not sure how to slim this down, i.e. currently the row/column headers are using the entire user / leave_type objects rather than a simple value, can I set column_name to user.name or similar?

@edjames
Copy link
Owner

edjames commented Apr 23, 2014

Hi
The usage section in the README states that your data should be a collection of ActiveRecord-like objects which respond to methods. Unfortunately a hash will not work.

Instead of running a raw sql query, I recommend you try using the ActiveRecord query interface and you will get some success.

So this won't work...

>> conn = ActiveRecord::Base.connection
>> conn.execute 'select id, name as user_name from users where id = 1'
   (0.4ms)  select id, name as user_name from users where id = 1
=> [{"id"=>1, "user_name"=>"Ed James", 0=>1, 1=>"Ed James"}]

...while this will work...

>> User.select('id, name as user_name').where(id: 1)
  User Load (0.2ms)  SELECT id, name as user_name FROM "users" WHERE "users"."id" = 1
+----+-----------+
| id | user_name |
+----+-----------+
| 1  | Ed James  |
+----+-----------+
1 row in set

Hope that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants