rails上的嵌套连接似乎不起作用

时间:2014-05-09 07:27:21

标签: ruby-on-rails join

所以,事情是这样的:

关系:

类型有很多程序, 程序属于类型

程序有很多组, 集团属于计划

输入型号:

class Type < ActiveRecord::Base
    has_many :programs
end

程序模型:

class Program < ActiveRecord::Base
    belongs_to :type
    has_many :groups
end

小组模特:

class Group < ActiveRecord::Base
    belongs_to :program
end

在我看来:

<% @Types.each do |type|%> //this works
   //Print some info about type

  <%type.programs.each do |program|%> //this also works
     //Print some info about this program, which depends of the type above

     <%program.groups.each do |group|%> //this doesnt work at all
         //Print some info about this group, which depends of the program above
     <%end%>    

  <%end%>

<%end%>

我得到的错误如下:

uninitialized constant Program::Groups

,应用程序跟踪是:

app/views/admin/index.html.erb:33:in `block (2 levels) in _app_views_admin_index_html_erb__4501256709281346516_69833887896820'
app/views/admin/index.html.erb:18:in `block in _app_views_admin_index_html_erb__4501256709281346516_69833887896820'
app/views/admin/index.html.erb:3:in `_app_views_admin_index_html_erb__4501256709281346516_69833887896820'

1 个答案:

答案 0 :(得分:0)

Program模型中

has_many :groups

在您看来应该是

program.groups.each do
你错过了这些&#39;分组