我的模型看起来像:
class Location < ActiveRecord::Base
attr_accessible :title
has_and_belongs_to_many :adjacent_locations, :class_name => "Location", :foreign_key => "adjacent_location_id", :join_table => "adjacent_locations_locations"
has_many :npcs #who are currently in this location
has_and_belongs_to_many :expected_npcs, :class_name => "Npc" #who do I expect to be here (is it their house?)
has_and_belongs_to_many :items #what do I actually have?
has_and_belongs_to_many :expected_items, :class_name => "Item"
has_and_belongs_to_many :expected_item_types, :class_name => "ItemType", :foreign_key => "e_item_type_id", :join_table => "e_item_types_locations"
end
除了“expected_item_types”之外,这些 has_and_belongs_to_many 中的每一个都有效。
它看起来就像模型中的相邻位置,但不起作用。
我的迁移运行得很好,据我所知,它看起来与其他连接表完全一样:
class CreateEItemTypesLocationsTable < ActiveRecord::Migration
def self.up
create_table :e_item_types_locations, :id => false do |t|
t.references :e_item_type
t.references :location
end
add_index :e_item_types_locations, [:e_item_type_id, :location_id], :name => "eit_loc"
end
def self.down
drop_table :e_item_types_locations
end
end
它有一个命名索引,就像 expected_items 一样,模型中有一个外键,就像 Location 一样。使用命名索引设置外键时有什么特别的事情要做吗?
我得到的实际错误是
SQLite3::SQLException: no such column: e_item_types_locations.item_type_id: SELECT "item_types".* FROM "item_types" INNER JOIN "e_item_types_locations" ON "item_types"."id" = "e_item_types_locations"."item_type_id" WHERE "e_item_types_locations"."e_item_type_id" = 1
这意味着它只是完全忽略了我设置的外键......这有什么明显的问题吗?
编辑:我想我至少想出了一部分,而不是外键,它应该是“association_foreign_key”。但是……那么……为什么我不必为相邻位置这样做呢?是因为他们和他们所在的模特是同一级别的吗?