Skip to content

Boolean / Integer search fails on postgres #1

@ConfusedVorlon

Description

@ConfusedVorlon

Hi,
I really like your approach here. I hope you have time to develop it further.

I hit an issue when running with postgres. The LIKE query fails for boolean or integer.
I'm guessing that you're testing on mysql or sqlite and they're more forgiving...

This fixes the issue for me at least:

    def filtered_resources(resources)
      @filterable_attributes = AdministrateFilterable::FiltererService.filter_attributes(dashboard, new_resource)

      filter_params = params[resource_name]
      return resources if filter_params.blank?

      filter_params.each do |key, value|
        next unless resources.column_names.include?(key.to_s) && value.present?
        attribute_type = new_resource.class.attribute_types[key].type

        case attribute_type
        when :boolean
          boolean_value = ActiveRecord::Type::Boolean.new.cast(value)
          resources = resources.where(key => boolean_value)
        when :integer
          integer_value = value.to_i
          resources = resources.where(key => integer_value)
        else
          sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, ["#{key} LIKE ?", "%#{value}%"])
          resources = resources.where(sanitized_query)
        end
      end

      resources
    end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions