I'm using ransack for a while and I just realised that using not_eq on an associated search is different and is making a weird NOT IN ... NOT request that seems to lead to error and make it difficult to debug.
Here is the ransack query I do, with the sql request it does
=> account.contacts.ransack(
g: {
'0' => {
"contact_fields_landing_page_field_definition_id_eq"=> "1432", "contact_fields_value_not_eq"=> "0"
}
}
).result.to_sql
SELECT `contacts`.*
FROM `contacts`
LEFT OUTER JOIN `contact_fields` ON `contact_fields`.`contact_id` = `contacts`.`id`
WHERE `contacts`.`account_id` = 1
AND (
`contact_fields`.`landing_page_field_definition_id` = 1432
AND `contacts`.`id`
NOT IN (
SELECT `contact_fields`.`contact_id` FROM `contact_fields`
WHERE `contact_fields`.`contact_id` = `contacts`.`id`
AND NOT (`contact_fields`.`value` != '0')
)
)
It return 540 contact with this request.
If I use classic activerecord query
=> account.contacts.left_joins(:contact_fields).where(
"contact_fields.landing_page_field_definition_id = 1432 AND contact_fields.value != '0'"
).to_sql
SELECT `contacts`.*
FROM `contacts`
LEFT OUTER JOIN `contact_fields` ON `contact_fields`.`contact_id` = `contacts`.`id`
WHERE `contacts`.`account_id` = 1
AND (contact_fields.landing_page_field_definition_id = 1432 AND contact_fields.value != '0')
This query give return me a total of 1940 contacts and this is exactly the number I'm looking for.
I don't get why I have such a big difference between the 2 requests