Rails:查询JSON数组的Postgres

时间:2016-02-23 11:07:24

标签: ruby-on-rails json postgresql activerecord

我使用Rails(4.2)和Postgres JSON数据类型来存储数组:

#<Mapping:0x007fa3b6263390 foo: ["bar", baz]>

现在我想查询具有特定数组的所有映射:

Mapping.where(foo: ["bar", "baz"])

原来这不起作用。我可以使用什么查询来查找我想要的内容?所有JSON查询示例通常用于散列,而不是数组(至少不在顶层)。

1 个答案:

答案 0 :(得分:0)

ActiveRecord为Postgres数组内置了support

Private Function GetValueOfMergedCells(sName, iColumn)
    Set myWorkbook = Workbooks().Worksheets()

    Set c = myWorkbook.Cells.Find(What:=sName, After:=ActiveCell, LookAt _
                        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

    If Not c Is Nothing Then
        If (iColumn <> 2) Then
            '...
        Else
            Set ma = NEWDevicesWS.Range("C2").MergeArea
            MsgBox ma.Cells(c.Row, 1).Value
            GetValueOfMergedCells = ma.Cells(c.Row, 1).Value
        End If
    Else
        '...
    End If
End Function


Sub main()
    Set firstWorkbook = Workbooks().Worksheets()

    ' Get array of strings
    Dim aNames() As String
        aNames = ImportSamplesNames() 

    For i = 1 To UBound(aNames())
        firstWorkbook.Cells(3 + i, 2) = GetValueOfMergedCells(aNames(i), 2)
    Next i
End Sub

有很多可用的数组函数,请查看here