Skip to content

Ransack doesn't lookup scope when using groupings #1490

@devsheva

Description

@devsheva

Ruby: 3.1.3
Rails: 7.0.4
Ransack: 3.2.1

Basic Setup

# -- Intervention Model --
class Intervention < ApplicationRecord

  belongs_to :father, class_name: "Intervention", foreign_key: "intervention_id", optional: true
  has_one :child, class_name: "Intervention", foreign_key: "intervention_id", dependent: :destroy

  scope :neither_father_nor_child, -> { where.missing(:father, :child) }

  def self.ransackable_scopes(auth_object = nil)
     %i[neither_father_nor_child]
  end
end

h = {:g=>{"0"=>{:neither_father_nor_child=>1}}}

Intervention.ransack(h) # => Ransack::Search<class: Intervention, base: Grouping <combinator: and>>
Intervention.ransack(h).result.to_sql # => SELECT \"interventions\".* FROM \"interventions\"

It is not calling the defined scope, but if i try

Intervention.ransack(neither_father_nor_child: 1) => # Ransack::Search<class: Intervention, scope: {"neither_father_nor_child"=>1}, base: Grouping <combinator: and>

So I don't know where is the problem

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions