我目前正在开发一种可安装的发动机.在发动机内我有以下两种型号:
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
任何人有类似的问题或我错过了什么?
解决方法
您可以显式设置连接表名称(
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