2012-02-18 72 views
1

好吧,我所得到的是兩種模式...如何計算滿足相關模型中條件的記錄?

Jiraissue:

class Jiraissue < ActiveRecord::Base 
    # JIRA uses a singular table name for this model 
    set_table_name 'jiraissue' 
    has_one :severity 
end 

嚴重性:

class Severity < ActiveRecord::Base 
    belongs_to :jiraissue 
end 

我想要做的就是讓所有的計數Jiraissues其中jiraissue.severity =「S1」

現在事實證明,jiraissue表有一個優先列,所以我可以在模型中拉這個技巧...

Jiraissue:

class Jiraissue < ActiveRecord::Base 
    # JIRA uses a singular table name for this model 
    set_table_name 'jiraissue' 
    has_one :severity 

    def self.count_priority(priority) 
    where("PRIORITY = ?",priority).count() 
    end 

end 

然後在視圖中做這樣的事情......

<%= (1..4).map { 
    |priority| Jiraissue.biit.bugs.recent.count_priority(priority) 
    }.inspect %> 

我怎樣做類似的Jiraissue獲得count_severity方法的東西嗎?

這是行不通的(我也不會指望它)...

def self.count_severity(severity) 
    where("severity = ?",severity).count() 
    end 

但我完全糊塗了。

+0

我太完全困惑了;)...''jiraissue.severity'會返回Severity類的關聯對象嗎?你如何比較'jiraissue.severity =「S1」'? – nkm 2012-02-18 04:10:41

回答

0
Jiraissue.joins(:severities).where(:severities => {:severity => "S1"}).count 
+0

Hooray !! Jiraissue.joins(:severity).where(:severities => {:severity =>「S1」})。count This TOTALLY works!感謝您讓我走上正確的道路! – 2012-02-23 22:45:39

0

模式

def self.count_priority(priority) 
    where("PRIORITY = ?",priority).size 
end 

控制器

def index 
    @jiraissues = Jiraissue.count_priority('S1') 
end 

不工作?

+0

不好,那不行,因爲優先級不是jiraissue表中的列。我想布恩的答案可能就是我正在尋找的東西。 – 2012-02-21 16:43:03

相關問題