2
의 3 수준의 합계는 나는이 모델을 가지고 :레일 : 내 레일 3.1 응용 프로그램에서 중첩 된 자원
class Project < ActiveRecord::Base
has_many :tasks
has_many :hours, :through => :tasks
def sum_hours
self.hours.sum(:hours)
end
end
class Task < ActiveRecord::Base
belongs_to :project
has_many :hours
def sum_hours
self.hours.sum(:hours)
end
end
class Hour < ActiveRecord::Base
attr_accessible :hours # column in table
belongs_to :task
end
projects_controller.rb이에 :
def show
@project = Project.find(params[:id])
@tasks = @project.tasks.includes(:hours)
end
및 프로젝트/show.html에서
....
<td>Sum hours: <%= @project.sum_hours %></td>
...
<% for task in @tasks do %>
<tr>
<td><%= task.sum_hours %></td>
</tr>
...
그러나 심지어 와 .includes (: 시간)이 여전히 사용을 ERB 나는이 사용 프로젝트와 모든 작업에 대해 별도로 쿼리합니다. 나는 무엇인가 놓치고 있냐?? 나는 실수하고 있니? 당신이 뭔가를 시도 할 수 있습니다
감사합니다. 덕분에 많은 도움이되었습니다. – Ryan