Skip to content

Relatedness is broken when not_in search with deep nesting #1411

@lydyoshitake

Description

@lydyoshitake

Relation is "Parent -> Child -> Grandchild". In this case, the association breaks down

Models

class Parent 
  ...
  has_many :childs
end

class Child 
  ...
  belongs_to :parent
  has_many :grandchilds
end

class Grandchild 
  ...
  belongs_to :child
end

Seach sample

Parent.ransack({childs_grandchilds_name_not_in: 'test'}).result.to_sql

Get SQL

SELECT `parents`.* 
FROM `parents`
LEFT OUTER JOIN `childs` ON `childs`.`parent_id` = `parents`.`id`
WHERE `parents`.`id` NOT IN (
  SELECT `grandchilds`.`child_id`
  FROM `grandchilds`
  WHERE `grandchilds`.`child_id` = `parents`.`id` 
    AND NOT (`grandchilds`.`name` NOT IN 'test')
)

I assume this is the outcome

SELECT `parents`.* 
FROM `parents`
LEFT OUTER JOIN `childs` ON `childs`.`parent_id` = `parents`.`id`
WHERE `childs`.`id` IN (
  SELECT `grandchilds`.`child_id`
  FROM `grandchilds`
  WHERE (`grandchilds`.`name` NOT IN 'test')
)

I think there is something wrong with the comparison of Parent.id and Child.id

Ruby: ruby 3.0.4p208 (2022-04-12 revision 3fa771dded) [x86_64-linux]
Rails: 6.1.6
Ransack: 3.2.1
DB: mysql Ver 14.14 Distrib 5.7.41, for Linux (x86_64) using EditLine wrapper

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions