I've rebuilt my network map and now I can't seem to get my nested model to work. It's set up as IDF => Switch (each IDF has many switches). I'm trying to be able list the switches of the IDF, but I get the following error:
Mysql2::Error: Unknown column 'switches.idf_id' in 'where clause': SELECT switches
.* FROM switches
WHERE switches
. idf_id
= 1
I'm assuming that for some reason when the mysql table is built for the switches there is no column made to map it to a switch id. I have no idea why it isn't. I've edited the models and re-rake the project a couple of times and I don't know what's missing. Any help would be much appreciated!
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 %>
^^Everything worked fine until I tried to add the switch functionality.
This sounds like an issue with your Switch database migration. Can you paste the migration? And are you generating migrations and models by hand, or using "rails generate ..."?
Your migration should look something like:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.