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

时间:2012-06-25 18:54:11

标签: ruby-on-rails ruby-on-rails-3 networking network-programming

我已经重建了我的网络地图,现在我似乎无法使我的嵌套模型起作用。它被设置为IDF =>开关(每个IDF有很多开关)。我试图能够列出IDF的开关,但是我收到以下错误:

Mysql2 ::错误:'where子句'中的未知列'switches.idf_id':SELECT switches。* FROM switches WHERE switchesidf_id = 1 < / p>

我假设由于某种原因,当为交换机构建mysql表时,没有列将其映射到交换机ID。我不知道为什么不是。我已经编辑了模型并重新调整了项目几次,我不知道缺少什么。任何帮助将不胜感激!

应用程序/模型/ idf.rb:

class Idf < ActiveRecord::Base
  attr_accessible :location, :room_number
  has_many :switches
  accepts_nested_attributes_for :switches
end

应用程序/模型/ switch.rb:

class Switch < ActiveRecord::Base
  attr_accessible :model, :title
  belongs_to :idf
end

应用程序/视图/ 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 %>

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

1 个答案:

答案 0 :(得分:1)

这听起来像是Switch数据库迁移的问题。你可以粘贴迁移吗? 您是手动生成迁移和模型,还是使用“rails generate ...”?

您的迁移应该类似于:

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