views:

207

answers:

1

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 select 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?

+2  A: 

Something like this should work:

t = Table(:translations)
c = t.where(t[:value].matches('%some search%')).project(:key)
t.where(t[:key].in(c))
valodzka
Hi Thanks for the answer.This piece of code does generate a sql statement that looks like it should do the thing, however mysql complains with error in syntax.It renders this: SELECT `translations`.* FROM `translations` WHERE (`translations`.`key` IN (SELECT key FROM `translations` WHERE `translations`.`value` LIKE '%value1%'))While it should render:SELECT `translations`.* FROM `translations` WHERE (`translations`.`key` IN (SELECT `translations`.`key` FROM `translations` WHERE `translations`.`value` LIKE '%value1%'))Is there any way of controlling this?
Mark Meeud
Use project(:'translations.key')
valodzka