Ruby on rails 使用动态值按键查询Postgres JSONB列

Ruby on rails 使用动态值按键查询Postgres JSONB列,ruby-on-rails,postgresql,jsonb,Ruby On Rails,Postgresql,Jsonb,我有一个modelUserPost,它有JSONB列posts,其中存储的数据将是JSON,其中user\u id作为键(动态),一些属性作为值。考虑到我有多个用户日志< /代码>下面的记录, 记录1: id: 1 posts: { 1: { message: 'some', posted_at: 'sometime', attachment_url: 'some_url' }, 3: { message: 'some', posted_at: 's

我有一个model
UserPost
,它有JSONB列
posts
,其中存储的数据将是JSON,其中
user\u id
作为键(动态),一些属性作为值。考虑到我有多个<代码>用户日志< /代码>下面的记录,

记录1:

 id: 1
 posts: { 
          1: { message: 'some', posted_at: 'sometime', attachment_url: 'some_url' },
          3: { message: 'some', posted_at: 'sometime', attachment_url: 'some_url' }
        }
记录2:

 id: 2
 posts: { 
   { 
      2: { message: 'some', posted_at: 'sometime', attachment_url: 'some_url' },
      5: { message: 'some', posted_at: 'sometime', attachment_url: 'some_url' }
    }
记录3:

id: 3
 posts: { 
   { 
      1: { message: 'some', posted_at: 'sometime', attachment_url: 'some_url' },
      2: { message: 'some', posted_at: 'sometime', attachment_url: 'some_url' }
    }
我有
user\u id
say
2,5
必须获取
UserPost
记录,我将如何做?

找到它

单键:

UserPost.where("posts ? '2'")
UserPost.where('posts ?| array[:keys]', keys: ['1', '2'])
密钥数组:

UserPost.where("posts ? '2'")
UserPost.where('posts ?| array[:keys]', keys: ['1', '2'])