![](/img/trans.png)
[英]How do I count the number of records that have one or more associated object?
[英]How do I count records that satisfy a condition in a associated model?
好的,我有兩個模型...
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
我正在嘗試獲取所有jiraissue.severity =“ S1”的Jiraissues的計數
現在事實證明,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
但是我完全感到困惑。
Jiraissue.joins(:severities).where(:severities => {:severity => "S1"}).count
模型
def self.count_priority(priority)
where("PRIORITY = ?",priority).size
end
控制者
def index
@jiraissues = Jiraissue.count_priority('S1')
end
不行嗎
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.