Rails实例变量nil仅在count时

时间:2016-11-12 17:33:58

标签: ruby-on-rails

我有一个表ticketlines,其中包含列:

ID, TICKET, PRODUCT, REASON, UNITS, PRICE

Controller Ticketlines:

  @q = Ticketline.joins(:product, :ticket).group(:PRODUCT).select("PRODUCT, products.CONSIGNOR, SUM(UNITS) AS TOTALUNITS").ransack(params[:q])
  @product_sales = @q.result.paginate(:page => params[:page], :per_page => 30)

在我看来,我想展示@product_sales.count

生成查询:

SELECT COUNT(*) AS count_all, `ticketlines`.`PRODUCT` AS ticketlines_product FROM `ticketlines` INNER JOIN `products` ON `products`.`ID` = `ticketlines`.`PRODUCT` INNER JOIN `tickets` ON `tickets`.`ID` = `ticketlines`.`TICKET` GROUP BY `ticketlines`.`PRODUCT`

显示@product_sales的结果数。

问题是当我尝试使用ransack过滤结果时。

产品表包含列ID, NAME, CATEGORY, CONSIGNOR

如果我按照PRODUCT过滤Ticketlines则可行。如果我按CATEGORY或CONSIGNOR过滤则不会。

例如,如果我在按@product_sales.count时按类别进行过滤,则会生成查询:

SELECT COUNT(DISTINCT ) AS count, `ticketlines`.`PRODUCT` AS ticketlines_product FROM `ticketlines` INNER JOIN `products` ON `products`.`ID` = `ticketlines`.`PRODUCT` INNER JOIN `tickets` ON `tickets`.`ID` = `ticketlines`.`TICKET` LEFT OUTER JOIN `categories` ON `categories`.`ID` = `products`.`CATEGORY` WHERE `categories`.`ID` = '289108c2-6be3-4baf-b053-96f70dc2cc5a' GROUP BY `ticketlines`.`PRODUCT`

对我来说没有意义的是,如果我从view @ product_sales.count中删除,则没有错误。我可以使用类别过滤器,它可以工作。

执行的查询是:

SELECT  PRODUCT, SUM(UNITS) AS TOTALUNITS FROM `ticketlines` INNER JOIN `products` ON `products`.`ID` = `ticketlines`.`PRODUCT` INNER JOIN `tickets` ON `tickets`.`ID` = `ticketlines`.`TICKET` LEFT OUTER JOIN `categories` ON `categories`.`ID` = `products`.`CATEGORY` WHERE `categories`.`ID` = '289108c2-6be3-4baf-b053-96f70dc2cc5a' GROUP BY `ticketlines`.`PRODUCT` LIMIT 30 OFFSET 0

只有在使用count时才能使@product_sales为零的原因是什么?

0 个答案:

没有答案