代码之家  ›  专栏  ›  技术社区  ›  Steven

MySQL COUNT结果在RoR中被解释为字符串而不是整数

  •  2
  • Steven  · 技术社区  · 14 年前

    这是我对好奇者的疑问。只有第一行和最后一行应该涉及这个问题:

    SELECT s.parent_id AS id, COUNT(s.parent_id) as count
      FROM items i
      LEFT OUTER JOIN (SELECT item_id, user_id, MAX(created_at) as created_at
        FROM item_views iv
        WHERE iv.user_id = ?
        GROUP BY item_id) AS v ON i.id = v.item_id
      LEFT OUTER JOIN categories c ON c.id = i.category_id
      INNER JOIN (SELECT node.id AS node_id, parent.title AS parent_title, parent.id AS parent_id
        FROM categories AS node, categories AS parent
        WHERE node.lft BETWEEN parent.lft AND parent.rgt) s ON s.node_id=i.category_id
      WHERE (i.updated_at > v.created_at OR v.created_at IS NULL)
      AND i.updated_at > ?
      GROUP BY s.parent_id;
    

    当我用 find_by_sql count 列似乎是字符串 "1" to_i

    1 回复  |  直到 14 年前
        1
  •  2
  •   Jed Schneider    14 年前

    您需要将返回的count属性转换为整数。

    find_by_sql

    http://apidock.com/rails/ActiveRecord/Base/find_by_sql/class