1
我需要使用Rails的Active Record寫下面的查詢:軌道上選擇屬性包括表
select c.name,p.name,SUM(e.hours)
from Events e, Projects p, Unities u, Clients c
where e.project_id = p.id AND
p.unity_id = u.id AND
u.client_id = c.id
group by c.name, p.name, e.project_id
型號:
class Event < ActiveRecord::Base
belongs_to :project
class Project < ActiveRecord::Base
belongs_to :unity
has_many :events
end
class Unity < ActiveRecord::Base
belongs_to :client
has_many :projects
end
class Client < ActiveRecord::Base
has_one :unity
end
我有類似:
Event.includes(project: [unity: :client])
但我不知道如何按部分指定屬性和組的選擇。
我在與你的建議如下因素的輸出:#<的ActiveRecord ::關係[#<事件ID:無>,#<事件ID:無> ]>。我如何獲得項目名稱,客戶名稱和活動時間的值?謝謝 – 2014-09-23 19:44:35