我目前正在开发一种可安装的引擎。在发动机内我有以下两种型号:
module Ems
class Channel < ActiveRecord::Base
has_and_belongs_to_many :categories
end
end
module Ems
class Category < ActiveRecord::Base
has_and_belongs_to_many :channels
end
end
这些是db迁移文件:
class CreateEmsChannels < ActiveRecord::Migration
def change
create_table :ems_channels do |t|
t.string :slug
t.string :name
t.timestamps
end
end
end
class CreateEmsCategories < ActiveRecord::Migration
def change
create_table :ems_categories do |t|
t.string :slug
t.string :name
t.text :strapline
t.timestamps
end
end
end
class CreateEmsCategoriesChannels < ActiveRecord::Migration
def up
# Create the association table
create_table :ems_categories_channels, :id => false do |t|
t.integer :category_id, :null => false
t.integer :channel_id, :null => false
end
# Add table index
add_index :ems_categories_channels, [:category_id, :channel_id], :unique => true
end
end
当我尝试检索关联的对象时,问题就开始了。
例如,当我调用@channel.get :categories
时,我收到以下错误:
Mysql2::Error: Table 'ems_development.categories_channels' doesn't exist:
SELECT `ems_categories`.*
FROM `ems_categories`
INNER JOIN `categories_channels`
ON `ems_categories`.`id` = `categories_channels`.`category_id`
WHERE `categories_channels`.`channel_id` = 1
正如你可以看到它缺少链接表上的isolate_namespace值,因为它应该查找表ems_categories_channels
上的关联而不是categories_channels
任何人都有类似的问题,或者我错过了什么?
答案 0 :(得分:6)
您可以显式设置连接表名称(per the documentation)。
module Ems
class Channel < ActiveRecord::Base
has_and_belongs_to_many :categories, :join_table => 'ems_categories_channels'
end
end
module Ems
class Category < ActiveRecord::Base
has_and_belongs_to_many :channels, :join_table => 'ems_categories_channels'
end
end