I have a Book class and need to implement a yes/no voting functionality. My domain classes look like this:
class Book {
String title
static hasMany = [votes: Vote]
}
class User {
String name
static hasMany = [votes: Vote]
}
class Vote {
boolean yesVote
static belongsTo = [user: User, book: Book]
}
What is the best way to implement a voting for the book class. I need the following informations:
What is the best way to implement the computation of the average yesVote such that the performance does not drop?
I would add a totalVotes to Book. Incremenent that for each yes vote. Then a simple count() of votes for a book along with the totalVotes value gives you what you need.
Update: Answering your comment questions:
def yesVotes = Vote.findAllByBookAndYesVote(bookInstance, Boolean.TRUE)
def votes = Vote.findAllByBook(bookInstance)
def userVote = Vote.findByUserAndBook(userInstance, bookInstance)