Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean / Integer search fails on postgres #1

Open
ConfusedVorlon opened this issue Feb 28, 2024 · 0 comments
Open

Boolean / Integer search fails on postgres #1

ConfusedVorlon opened this issue Feb 28, 2024 · 0 comments

Comments

@ConfusedVorlon
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant