rails 3 group_by已加入表格

时间:2011-03-12 07:06:48

标签: ruby-on-rails activerecord group-by

我正努力让我的第一个小组去上班。

该模型有4个表

class Capstone < ActiveRecord::Base

  has_many :capstone_milestones
  has_many :milestones, :through => :capstone_milestones

end

class CapstoneMilestone < ActiveRecord::Base

  belongs_to :milestone
  belongs_to :capstone

end

class Milestone < ActiveRecord::Base

  has_many :capstone_milestones
  has_many :capstones, :through => :capstone_milestones
  belongs_to :department

end

class Department < ActiveRecord::Base

  has_many :milestones
  attr_accessible :id, :name

end

我希望显示已定义部门的所有里程碑,并按顶点

对其进行分组

要做到这一点,我在我的部门控制器

中做了这个
  def show
    @department = Department.find(params[:id])
    @milestones = Milestone.where("department_id = ?", params[:id] )
    @capstones = @milestones.group_by { |m| m.capstones.name }

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @department }
    end
  end

这在我看来

     <table>
        <% @capstones.each do |capstone, milestones|  %>

          <tr>
            <td colspan="2">
              <%=link_to capstone.name, capstone_path(:id => capstone.id)%>
            </td>
          </tr>
          <% for milestone in milestones  %>
            <tr>
              <td witdth="10px">&nbsp;</td>
              <td> <%=link_to milestone.name, milestone_path(:id => milestone.id)%></td>

            </tr>
          <% end %>

        <% end %>
    </table>

返回以下错误语句:“undefined local variable or method`name'”

怎么了?谢谢!

0 个答案:

没有答案