0
組我有以下3個模型和關係之間:在日曆ROR協會通過查詢
class Calendar < ActiveRecord::Base
has_many:fiscalcalendars
has_many:voucherdatas ,:through => :fiscalcalendars
end
class Fiscalcalendar < ActiveRecord::Base
belongs_to :calendar
has_many :voucherdatas
end
class Voucherdata < ActiveRecord::Base
has_many :fiscalcalendars
has_many :calendars, :through => :fiscalcalendars
end
- 字段:ID,MONTHNAME在fiscalcalendar
- 字段:ID,CALENDAR_ID,fiscalyearweek
- voucherdata中的字段:id,vhour,fiscalyear周
我想要每個月的vhour總和
我可以通過財政星期做
Voucherdata.sum(:vhour,:group=>:fiscalyearweek)
有沒有更簡單的方式,通過每月能拿到它去組?
查詢下面生成;請注意,因爲它沒有fiscalcalenar.voucherdat_id列因此它不會wrk .. thx爲您的時間 SELECT sum(vhour)AS sum_vhour,calendars.name AS calendars_name FROM`voucherdatas` INNER JOIN`fiscalcalendars` ON(`voucherdatas` (``calendars`.`id` =`fiscalcalendars`.`calendar_id`)GROUP BY calendars.name – philipth 2009-10-09 13:37:49