Reputation: 59
Why the "and" and "or" clauses don't work inside the findAll() method in grails?
For instance, this code:
Student.findAll {
and {
name == "A"
name ==~ "%A"
}
}.collect { it.name }
will generate the list:
['AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ']
which is wrong. It should have generated nothing. None of the records match the condition having the exact name “A” and an ending “A”. The string shown is listing ALL the records in my Student table.
It gets a little bit worse. This code:
Student.findAll {
or {
name == "A"
name ==~ "%A"
}
}.collect { it.name }
generates the same list:
['AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ']
when in reality should have returned just the record “AA”.
Is there something wrong with this code?
Thanks!
Upvotes: 1
Views: 1139
Reputation: 66069
Use regular boolean operators in the criteria, e.g.:
Student.findAll {
name == "A" && name ==~ "%A"
}.collect { it.name }
Student.findAll {
name == "A" || name ==~ "%A"
}.collect { it.name }
Upvotes: 1