ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Ransack doesn't lookup scope when using groupings

Open devsheva opened this issue 1 year ago • 2 comments

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

devsheva avatar Apr 30 '24 15:04 devsheva

I'm having the same problem.

librod89 avatar Jun 27 '24 17:06 librod89

Maybe cause it would be wrong to use a scope as hash key in a grouping?

devsheva avatar Jun 28 '24 20:06 devsheva