Sha256: 82dd3fdeb44523b53c6220a3ea4b797bf436dff10ac50a96b3b7658c226a8bd6

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

---
sidebar_position: 1
title: Custom predicates
---

If you'd like to add your own custom Ransack predicates:

```ruby
# config/initializers/ransack.rb

Ransack.configure do |config|
  config.add_predicate 'equals_diddly', # Name your predicate
    # What non-compound ARel predicate will it use? (eq, matches, etc)
    arel_predicate: 'eq',
    # Format incoming values as you see fit. (Default: Don't do formatting)
    formatter: proc { |v| "#{v}-diddly" },
    # Validate a value. An "invalid" value won't be used in a search.
    # Below is default.
    validator: proc { |v| v.present? },
    # Should compounds be created? Will use the compound (any/all) version
    # of the arel_predicate to create a corresponding any/all version of
    # your predicate. (Default: true)
    compounds: true,
    # Force a specific column type for type-casting of supplied values.
    # (Default: use type from DB column)
    type: :string,
    # Use LOWER(column on database).
    # (Default: false)
    case_insensitive: true
end
```
You can check all Arel predicates [here](https://github.com/rails/rails/blob/main/activerecord/lib/arel/predications.rb).

If Arel does not have the predicate you are looking for, consider monkey patching it:

```ruby
# config/initializers/ransack.rb

module Arel
  module Predications
    def gteq_or_null(other)
      left = gteq(other)
      right = eq(nil)
      left.or(right)
    end
  end
end

Ransack.configure do |config|
  config.add_predicate 'gteq_or_null', arel_predicate: 'gteq_or_null'
end
```

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ransack-4.2.1 docs/docs/going-further/custom-predicates.md
ransack-4.2.0 docs/docs/going-further/custom-predicates.md
ransack-4.1.1 docs/docs/going-further/custom-predicates.md
ransack-4.1.0 docs/docs/going-further/custom-predicates.md
ransack-4.0.0 docs/docs/going-further/custom-predicates.md
ransack-3.2.1 docs/docs/going-further/custom-predicates.md
ransack-3.2.0 docs/docs/going-further/custom-predicates.md
ransack-3.1.0 docs/docs/going-further/custom-predicates.md
ransack-3.0.1 docs/docs/going-further/custom-predicates.md
ransack-3.0.0 docs/docs/going-further/custom-predicates.md