我想計算所有受傷人羣的百分比,但無法弄清楚。。其中嵌套屬性屬性的屬性=字符串
損傷模型:
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