Last active
October 3, 2024 13:02
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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