Ruby on rails 如何在RubyonRails中查看嵌套模型?

Ruby on rails 如何在RubyonRails中查看嵌套模型?,ruby-on-rails,ruby-on-rails-3,networking,network-programming,Ruby On Rails,Ruby On Rails 3,Networking,Network Programming,我已经重建了我的网络地图,现在我似乎无法让我的嵌套模型工作。它设置为IDF=>Switch每个IDF都有许多交换机。我试图列出IDF的开关,但出现以下错误: Mysql2::错误:“where子句”中的未知列“switches.idf_id”:从switches where switches.idf_id=1中选择switches.* 我假设出于某种原因,当为交换机构建mysql表时,没有将其映射到交换机id的列。我不知道为什么没有。我已经编辑了模型并重新整理了项目好几次,我不知道遗漏了什么。任

我已经重建了我的网络地图,现在我似乎无法让我的嵌套模型工作。它设置为IDF=>Switch每个IDF都有许多交换机。我试图列出IDF的开关,但出现以下错误:

Mysql2::错误:“where子句”中的未知列“switches.idf_id”:从switches where switches.idf_id=1中选择switches.*

我假设出于某种原因,当为交换机构建mysql表时,没有将其映射到交换机id的列。我不知道为什么没有。我已经编辑了模型并重新整理了项目好几次,我不知道遗漏了什么。任何帮助都将不胜感激

app/models/idf.rb:

class Idf < ActiveRecord::Base
  attr_accessible :location, :room_number
  has_many :switches
  accepts_nested_attributes_for :switches
end
app/models/switch.rb:

class Switch < ActiveRecord::Base
  attr_accessible :model, :title
  belongs_to :idf
end
app/views/idfs/show.html.erb:

<p id="notice"><%= notice %></p>

<p>
  <b>Location:</b>
  <%= @idf.location %>
</p>

<p>
  <b>Room number:</b>
  <%= @idf.room_number %>
</p>

<h2>Switches:</h2>
<%= render @idf.switches %>

<h2>Add a switch:</h2>
<%= render "switches/form" %>

<%= link_to 'Edit', edit_idf_path(@idf) %> |
<%= link_to 'Back', idfs_path %>

^^在我尝试添加交换机功能之前,一切正常。

这听起来像是交换机数据库迁移的问题。你能粘贴迁移吗? 您是手动生成迁移和模型,还是使用rails生成

您的迁移应该如下所示:

class AddSwitch < ActiveRecord::Migration

  #assuming Rails 3
  def change
    create_table :switches do |t|
      # Add attributes
      t.references :idf  # same as t.integer :idf_id
    end
  end

end

啊,非常感谢你。我马上就去试试,但我相信你是对的。我完全忘了在我的脚手架代码中添加idf:references。