Ransack: Ransack automatic convert string 'f' or 't' to boolean

Created on 18 Jan 2019  路  5Comments  路  Source: activerecord-hackery/ransack

I know that this is a problem from Ransack but I still not find solution. When I search and pass parameter as 't' or 'f' it automatically convert to Boolean and my search is wrong.

Most helpful comment

I found the same bug, in the README it refers there is a automatic conversion for boolean values on a string or integers, but it also seems to do the same for t and f

In Rails 3 and 4, if the true value is being passed via url params or some other mechanism that will convert it to a string, the true value may not be passed to the ransackable scope unless you wrap it in an array (i.e. activated: ['true']). Ransack will take care of changing 'true' into a boolean. This is currently resolved in Rails 5 馃槂

However, perhaps you have user_id: [1] and you do not want Ransack to convert 1 into a boolean. (Values sanitized to booleans can be found in the constants.rb). To turn this off globally, and handle type conversions yourself, set sanitize_custom_scope_booleans to false in an initializer file like config/initializers/ransack.rb:

Ransack.configure do |c|
  c.sanitize_custom_scope_booleans = false
end

To turn this off on a per-scope basis Ransack adds the following method to ActiveRecord::Base that you can redefine to selectively override sanitization:

ransackable_scopes_skip_sanitize_args

Add the scope you wish to bypass this behavior to ransackable_scopes_skip_sanitize_args:

def ransackable_scopes_skip_sanitize_args
  [:scope_to_skip_sanitize_args]
end

https://github.com/activerecord-hackery/ransack#using-scopesclass-methods

All 5 comments

I found the same bug, in the README it refers there is a automatic conversion for boolean values on a string or integers, but it also seems to do the same for t and f

In Rails 3 and 4, if the true value is being passed via url params or some other mechanism that will convert it to a string, the true value may not be passed to the ransackable scope unless you wrap it in an array (i.e. activated: ['true']). Ransack will take care of changing 'true' into a boolean. This is currently resolved in Rails 5 馃槂

However, perhaps you have user_id: [1] and you do not want Ransack to convert 1 into a boolean. (Values sanitized to booleans can be found in the constants.rb). To turn this off globally, and handle type conversions yourself, set sanitize_custom_scope_booleans to false in an initializer file like config/initializers/ransack.rb:

Ransack.configure do |c|
  c.sanitize_custom_scope_booleans = false
end

To turn this off on a per-scope basis Ransack adds the following method to ActiveRecord::Base that you can redefine to selectively override sanitization:

ransackable_scopes_skip_sanitize_args

Add the scope you wish to bypass this behavior to ransackable_scopes_skip_sanitize_args:

def ransackable_scopes_skip_sanitize_args
  [:scope_to_skip_sanitize_args]
end

https://github.com/activerecord-hackery/ransack#using-scopesclass-methods

@truonglocbinh93 can this be closed?

@seanfcarroll Yes It can be close, thank you

Closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanfcarroll picture seanfcarroll  路  4Comments

zenati picture zenati  路  4Comments

mbajur picture mbajur  路  5Comments

AnnaErshova picture AnnaErshova  路  3Comments

MatsumotoHiroko picture MatsumotoHiroko  路  4Comments