ransack icon indicating copy to clipboard operation
ransack copied to clipboard

How can I use one input field to chain 2 predicates?

Open benlieb opened this issue 3 years ago • 1 comments

I am trying to have one input field for either an id_eq or name_cont.

Currently id_or_departure_name_cont produces this in the query, where the string id is being cast to a 0 and being compared via LIKE, which clearly does not work for my purposes.

WHERE ((`departure`.`id` LIKE 0 OR `departure`.`departure_name` LIKE '%26053%')

But using id_eq_or_departure_name_cont raises exception.

How can I use one field for this type of query?

Thanks for the amazing work!

benlieb avatar Jun 02 '21 16:06 benlieb

Something like this

  scope :search_by, ->(term) do
    where("id = ? OR departure_name LIKE ?", term, "%#{term}%")
  end

  def self.ransackable_scopes(_auth_object = nil)
    [:search_by]
  end

  def self.ransackable_scopes_skip_sanitize_args
    [:search_by]
  end

stereobooster avatar Nov 12 '22 11:11 stereobooster