【问题标题】:Rails: searching with a wildcard within a Postgres string arrayRails:在 Postgres 字符串数组中使用通配符进行搜索
【发布时间】:2015-05-29 21:17:21
【问题描述】:

我们的应用运行 Rails 4.2 和 Postgres,并使用字符串数组。例如:

add_column :things, :identifiers, :string, array: true, default: []

Thing.create(identifiers: ['blahsomething', 'other'])
Thing.create(identifiers: ['blahother', 'yetanother'])

我想使用 ActiveRecord 来查询标识符为 LIKE 'blah%' 的所有事物。

对于原始 SQL,我相信 Postgres 的 unnest 命令可以做到这一点:Postgres Query of an Array using LIKE

但是如何通过 ActiveRecord 范围查询字符串数组中的 LIKE,以便可以与其他 ActiveRecord 查询链接呢?

【问题讨论】:

    标签: ruby-on-rails postgresql activerecord


    【解决方案1】:

    为此,我做了 2 个查询。我创建了一张表:

    sti_development=> select * from posts;
     id |            tags             |         created_at         |         updated_at         
    ----+-----------------------------+----------------------------+----------------------------
      1 | {blahsomething,other}       | 2015-05-30 06:08:51.668394 | 2015-05-30 06:08:51.668394
      2 | {blahother,yetanother}      | 2015-05-30 06:09:12.350234 | 2015-05-30 06:09:12.350234
      3 | {otherblahother,yetanother} | 2015-05-30 06:09:32.534039 | 2015-05-30 06:09:32.534039
    (3 rows)
    

    这是我的 Rails 查询:

    [arup@sti (master)]$ rails c
    Loading development environment (Rails 4.2.1)
    => Unable to load pry
    >> Post.where(id: Post.select("distinct x.id").from(Post.select("id, unnest(tags) tag"), :x).where("x.tag like ?", 'blah%').pluck("x.id"))
       (1.3ms)  SELECT x.id FROM (SELECT id, unnest(tags) tag FROM "posts") x WHERE (x.tag like 'blah%')
      Post Load (1.4ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" IN (1, 2)
    => #<ActiveRecord::Relation [#<Post id: 1, tags: ["blahsomething", "other"], created_at: "2015-05-30 06:08:51", updated_at: "2015-05-30 06:08:51">, #<Post id: 2, tags: ["blahother", "yetanother"], created_at: "2015-05-30 06:09:12", updated_at: "2015-05-30 06:09:12">]>
    

    Rails 确实支持from 方法来支持内部查询。

    另一种方法是选择分隔符,它不是标签的一部分。如下:

    sti_development=> SELECT "posts".* from "posts" WHERE ('|' || array_to_string(tags, '|')) LIKE ('%|' || 'blah%');
     id |             tags             |         created_at         |         updated_at
    ----+------------------------------+----------------------------+----------------------------
      1 | {blahsomething,other}        | 2015-05-30 06:08:51.668394 | 2015-05-30 06:08:51.668394
      2 | {blahother,yetanother}       | 2015-05-30 06:09:12.350234 | 2015-05-30 06:09:12.350234
      4 | {otherblahother,blahanother} | 2015-05-30 06:46:12.929428 | 2015-05-30 06:46:12.929428
    (3 rows)
    
    sti_development=> \q
    

    现在在 Rails 中。

    [arup@sti (master)]$ rails c
    Loading development environment (Rails 4.2.1)
    => Unable to load pry
    >> Post.where("'|' || array_to_string(tags, '|') LIKE ? ", "%|blah%")
      Post Load (1.5ms)  SELECT "posts".* FROM "posts" WHERE ('|' || array_to_string(tags, '|') LIKE '%|blah%' )
    => #<ActiveRecord::Relation [#<Post id: 1, tags: ["blahsomething", "other"], created_at: "2015-05-30 06:08:51", updated_at: "2015-05-30 06:08:51">, #<Post id: 2, tags: ["blahother", "yetanother"], created_at: "2015-05-30 06:09:12", updated_at: "2015-05-30 06:09:12">, #<Post id: 4, tags: ["otherblahother", "blahanother"], created_at: "2015-05-30 06:46:12", updated_at: "2015-05-30 06:46:12">]>
    >>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      相关资源
      最近更新 更多