У меня есть этот запрос, который, похоже, работает в pgAdmin. Но когда я пытаюсь перевести его на использование scuttle, я получаю массивный фрагмент запроса, содержащий всю инструкцию case. Как мне реорганизовать этот запрос, чтобы я мог использовать его в Arel?Как реорганизовать запрос, когда я сортирую продукты по его отношениям из другой таблицы
SELECT products.*, case when
(select custom_fields.value_text
from custom_fields
where custom_fields.custom_field_definition_id = 4
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is null
then 'stopped'
when
(select custom_fields.value_text
from custom_fields
where custom_fields.custom_field_definition_id = 4
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is not null
then (select custom_fields.value_text from custom_fields where custom_fields.custom_field_definition_id = 4
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product')
end as sorted
from products
order by sorted
Дополнительная информация: Я создал SQLite базу данных, которая показывает ожидаемое поведение и могут быть использованы для дальнейших экспериментов.
https://github.com/bigos/rails-sorting/blob/master/README.org
Первоначальный вывод: Я нашел способ, чтобы получить ожидаемые результаты, но больше не может использовать его с активными методами Record. К счастью, я нашел, как разбивать массив хешей.
Наилучшее решение: Я могу вернуть правильные отношения активной записи, если я поместил свой код в качестве подзапроса в порядке.
SELECT products.*
FROM products
ORDER BY (case when
(select custom_fields.value_text
from custom_fields
where custom_fields.custom_field_definition_id = 4
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is null
then 'stopped'
when
(select custom_fields.value_text
from custom_fields
where custom_fields.custom_field_definition_id = 4
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product') is not null
then (select custom_fields.value_text from custom_fields where custom_fields.custom_field_definition_id = 4
and custom_fields.table_record_id = products.id and custom_fields.belongs_to_table = 'product')
end)
Пожалуйста, предоставьте исходные данные и ожидаемый результат. – vyegorov
На данный момент я использовал объединение двух запросов. Через две недели у меня будет обзор кода, и тогда мы решим, что дальше. –
этот, кажется, работает ap Product.all.order (% Q | coalesce ((SELECT "custom_fields". "Value_text" FROM "custom_fields" WHERE ("custom_fields". "Table_record_id" = "products". "Id" AND " custom_fields "." value_text "IS NOT NULL))," остановлен ") |) –