Skip to content

Instantly share code, notes, and snippets.

@gangelo
Last active October 3, 2024 13:02
Show Gist options
  • Save gangelo/65e21f95ffd39dffd0c6e5a36cb4a772 to your computer and use it in GitHub Desktop.
Save gangelo/65e21f95ffd39dffd0c6e5a36cb4a772 to your computer and use it in GitHub Desktop.
Arel method that dynamically creates a blank condition named function that can me used to check a table column for blank?
def blank_condition_for(model:, attr:)
table = model.arel_table
trimmed_attr_value = Arel::Nodes::NamedFunction.new('TRIM', [table[attr]])
table[attr].eq(nil).or(trimmed_attr_value.eq(''))
end
# Usage
model = User
attr = :edipi
blank_condition = blank_condition_for(model:, attr:)
User.where(blank_condition)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment