【问题标题】:Nested joins on rails doesn't seems to work轨道上的嵌套连接似乎不起作用
【发布时间】:2014-06-26 20:33:28
【问题描述】:

所以,事情是这样发展的:

关系:

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

程序有很多组, 组属于程序

型号:

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'

【问题讨论】:

  • 试试 program.groups 而不是 program.group
  • index.html.erb 中的哪一行的编号为 33?另外,编辑您的视图代码以修复group 错字。
  • ,说“//这根本不起作用”的那个n_n
  • 所以你在这一行有&lt;% program.group.each do |group| %&gt;,对吧?
  • 是的,就是那个

标签: ruby-on-rails join


【解决方案1】:

在您的Program 模型中

has_many :groups

在你看来应该是

program.groups.each do

你错过了分组中的“s”

【讨论】:

  • @wcsantos 如果是这样,请分别编辑您的问题。并添加Program模型的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 2011-09-14
  • 2017-08-04
  • 1970-01-01
相关资源
最近更新 更多