2017-01-27 82 views
0

我有一個基於續集的類,我需要做一些總結。如何使用續集寶石獲得group_and_count的結果?

我正在做group_and_count,我可以看到它正在生成正確的查詢。然而,當我嘗試訪問的結果,續集試圖要挾行到類,我訪問過:

[33] pry(main)> grouped = Pancakes::Stack.active.group_and_count('health_state') 
=> #<Sequel::Mysql2::Dataset: "SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state'"> 
[34] pry(main)> grouped.each_entry { |row| puts row } 
I sequel: (0.001344s) SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state' 
#<Pancakes::Stack:0x000000089251a0> 
=> #<Sequel::Mysql2::Dataset: "SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state'"> 
[35] pry(main)> grouped.first 
I sequel: (0.001502s) SELECT 'health_state', count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY 'health_state' LIMIT 1 
I sequel: (0.001243s) SELECT * FROM `pancakes_stacks` WHERE (`id` IS NULL) LIMIT 1 
=> #<Pancakes::Stack:0x44b068c> 

我能得到什麼,我需要通過周圍的ORM的東西的工作,但似乎需要我重新實現active上述方法,並找出如何從類名獲得表名:

[38] pry(main)> groupie = grouped.db[:pancakes_stacks].where(deleted_at:nil).group_and_count(:health_state) 
=> #<Sequel::Mysql2::Dataset: "SELECT `health_state`, count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY `health_state`"> 
[39] pry(main)> groupie.each_entry { |row| puts row } 
I sequel: (0.001598s) SELECT `health_state`, count(*) AS `count` FROM `pancakes_stacks` WHERE (`deleted_at` IS NULL) GROUP BY `health_state` 
{:health_state=>nil, :count=>3} 
{:health_state=>"healthy", :count=>10} 

是不是有一個更簡單的方法?我在querying page上花了很多時間,但沒有一個例子顯示如何訪問結果。

回答

0

您可能只是想爲您的數據集添加一個.naked,這會使數據集返回哈希而不是模型對象。