I have 2 simple domain models. I am trying to get a count on publications which have the published flag set to NO from within the show page of the portfolio instance.
I can get the total number of publications set at NO, but cannot get the instances particular count of NO flags. Regardless of what I have tried, I always get a total back from all records. How can I get a instance count only.
example: user loads the show page(action) for Porfolio, a list of publications is shown, a number field showing publications which have not been published by counting "published" flag NO for that particular Portfolio.
I have tried Publication.countByPublished('No'), ${Publication.executeQuery("select count (published) from Publication where published=?",['No'])} and various other methods, but just cant seem to pass that portfolio id in so it counts only that id's publications where published flag is No.
Really scratching my head on this one
Class Portfolio
hasMany = [publications:Publication]
Sring portfolioName
Class Publication
belongsTo = [portfolio: Portfolio]
String publicationName
String publicationContent
String published
static constraints = {
published (blank: false, inList: ["No","Yes"])
Have you tried:
def count = Publication.countByPublishedAndPortfolio("No", portfolioInstance)
A less effeciant way would be to iterate over the portfolios collection of publications and keep a counter:
def count = 0
portfolioInstance.publications.each {
if (it.published == 'No') {
count++
}
}