[Ransack] Watch out for ransackable_scopes!

Conclusion

If the argument passed to scope executed by ransackable_scopes is the following value ArgumentError wrong number of arguments (given 0, expected 1) Be careful because it will occur! !! !!

These values are ** implicitly converted ** to True and False, respectively, and cannot be passed as arguments.

solution

config/initializers/ransack.rb


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

Just add this and you will be able to pass all the above values **. [^ 1]

If you want to customize the values you can pass

** Please give up. ** ** The implicitly converted value is Ransack :: Constants :: BOOLEAN_VALUES Defined in [^ 2]. This constant is freeze. According to the official, there is no way to ** unfreeze ** a freeze object. [^ 3]

There is no way to unfreeze a frozen object.

I did a lot of research on customizing the values to be converted, but I couldn't. .. .. For the time being, PR of the content "I made it possible to customize the value to be implicitly converted!" was merged. .. According to this, it seems that you can customize the value to be converted as follows.

Ransack.configure do |config|
   config.truthy_values_to_convert_in_custom_scopes = ['TRUE', 'true', '1']
   config.falsey_values_to_convert_in_custom_scopes = ['FALSE', 'no way no how']
end

** However, the following error occurred even if I wrote it as it was. ** **

=> NoMethodError: undefined method `truthy_values_to_convert_in_custom_scopes' for Ransack:Module

**What! ?? !! ?? !! ?? !! ?? !! ?? !! ?? !! ?? !! ?? !! ?? !! ?? !! ?? !! ?? ** ** I checked PR implementation contents, but the contents as described in the overview were not implemented. .. .. .. Please let me know if anyone knows here. .. ..

Also, @t_oginogin has suggested a solution that does not use Ransack, so please refer to it according to your implementation status. https://qiita.com/t_oginogin/items/b45636d64c271ebc409c

References

[^ 1]: About sanitize_custom_scope_booleans (Ransack official) https://github.com/activerecord-hackery/ransack#using-scopesclass-methods

[^ 2]: Contents of BOOLEAN_VALUES (Ransack official) https://github.com/activerecord-hackery/ransack/blob/c9cc20de9e0f7bab92e0579c85bed64d614d23de/lib/ransack/constants.rb#L26

[^ 3]: Cannot unfreeze (Ruby official) https://ruby-doc.org/core-2.6.6/Object.html#method-i-freeze

Recommended Posts

[Ransack] Watch out for ransackable_scopes!
Watch out for embedded variables in S2Dao
Things to watch out for in Java equals
Things to watch out for when creating a framework
Things to watch out for in future Java development
Things to watch out for when using Deeplearning4j Kmeans
Find out about annotationProcessor for Gradle 5.0