James McMahon
James McMahon

Reputation: 49629

Is there a way to do null safe GORM dynamic finders?

Is there a way ensure null safety in a GORM dynamic finder?

If I wanted to do something like Book.findByName(author.bookname), I could guard against null authors by changing it to Book.findByName(author?.bookname) but how do I avoid an exception on nulls?

Book.findByName() is applicable for argument types: () values: []

Upvotes: 0

Views: 558

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

auther?.bookname ? Book.findByName(author.bookname) : null //or do nothing

Based on the fact from the example that bookname is NOT NULL.

or you can use find

Book.find {name == author?.bookname} //Returns null if no records found

Upvotes: 1

Related Questions