为此,我做了 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">]>
>>