开发者

Rails3 and Arel to select using IN and subselect

开发者 https://www.devze.com 2023-01-11 02:58 出处:网络
I have a table called translations. (And a correspoding ActiveRecord class). This table contains the following fields

I have a table called translations. (And a correspoding ActiveRecord class). This table contains the following fields id, key and value

I would like to s开发者_JAVA百科elect all translations where the key matches a given query + all the translations that don't match the query, but share the key with a translation which does matches the query.

The resulting SQL could look something like this:

SELECT * FROM TRANSLATIONS where key in
    (select key from Translations where value like '%some search%')

I've tried a few things, but I can't seem to figure it out. Any ideas on how to express this in Arel?


Something like this should work:

t = Table(:translations)
c = t.where(t[:value].matches('%some search%')).project(:key)
t.where(t[:key].in(c))


Similar to @valodzka, but add "t[....]" around the :key symbol

t = Table(:translations)
c = t.where(t[:value].matches('%some search%')).project(t[:key])
t.where(t[:key].in(c))
0

精彩评论

暂无评论...
验证码 换一张
取 消