2013-02-07 101 views
0

我有2個簡單的域模型。我試圖從發佈組合實例的顯示頁面中將發佈標誌設置爲NO的發佈計數。Grails - countBy在_template中統計所有記錄而不是域實例

我可以得到設置爲NO的發佈總數,但無法獲取NO標誌的特定計數。無論我嘗試過什麼,我總是從所有記錄中獲得回報。我怎樣才能得到一個實例數。

示例:用戶加載Porfolio的顯示頁面(操作),顯示出版物清單,數字字段顯示未通過計算該特定投資組合的「已發佈」標誌NO發佈的出版物。

我試過了Publication.countByPublished('No'),$ {Publication.executeQuery(「select count(published)from published where =?」,['No'])}和其他各種方法,不能似乎傳遞組合ID中,因此僅計算ID的出版物,其中刊登標誌號

真的抓我的頭就這一個

類組合

hasMany = [publications:Publication] 

Sring portfolioName 

類出版物

belongsTo = [portfolio: Portfolio] 

String publicationName 
String publicationContent 
String published 

static constraints = { 
published (blank: false, inList: ["No","Yes"]) 

回答

2

你試過:

def count = Publication.countByPublishedAndPortfolio("No", portfolioInstance) 

一個不太effeciant辦法是遍歷出版物的組合收集和保留一個計數器:

def count = 0 
portfolioInstance.publications.each { 
    if (it.published == 'No') { 
    count++ 
    } 
} 
+0

在控制器I在Portfolio show.gsp中有上面的方法我有$ {count} – IanN

+0

什麼方法和什麼問題?它不起作用? – Gregg

+1

對不起,編輯,def count =出版...在投資組合控制器與show.gsp $ {count}是否正確? – IanN

相關問題