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

Rails如何运行自定义查询并返回映射数组

  •  0
  • TrongBang  · 技术社区  · 7 年前

    select attributeA1, attributeA2, attributeB2 from ModelA inner join ModelB on ModelA.id == ModelB.id
    

    最终的结果是我想要一个数组。每个元素都是一个带有以下键的映射:attributeA1、:attributeA2、:attributeB2。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Maksim P    7 年前
        2
  •  0
  •   TrongBang    7 年前

    答案如下。

      connection = ActiveRecord::Base.connection
      res = connection.exec_query(%Q(
        select attributeA1, attributeA2, attributeB2 
           from ModelA inner join ModelB 
           on ModelA.id == ModelB.id
      ))
    

    res.rows res.columns

    推荐文章