我想计算所有受伤人群的百分比,但无法弄清楚。。其中嵌套属性属性的属性=字符串
损伤模型:
class Injury < ActiveRecord::Base
belongs_to :player
belongs_to :season
has_one :injury_type
end
InjuryType型号:
class InjuryType < ActiveRecord::Base
has_one :body_group
end
BodyGroup模型(无关联):
class BodyGroup < ActiveRecord::Base
end
我打印受伤的总数是这样的:
= Injury.all.order('start_date ASC').count
... 但后来我卡住了。在我看来,这应该是超级简单的,如下所示:
= Injury.where(injury_type.body_group_id => 1).count
我该如何打印身体组有id 1的受伤人数?我究竟做错了什么?我已经试过这样:
= Injury.joins(:body_group => :injury_type).where(:body_group => {:id => 1})
...但这只是给了我#<Injury::ActiveRecord_Relation:0x007ff14c929bf0>
,我不能变成一个.Count之间或的.sum。
你应该在'where'子句中使用的表的名称,以及'includes' /'joins'中的关系名称(请参阅http://stackoverflow.com/questions/23633301/how-to-query-a-model-based-on-attribute-of-another-model-这属于这个fi/23633352#23633352和类似的问题链接在这个答案) – MrYoshiji