ruby – HABTM链接表未在可安装引擎中获取isolate_namespace值

我目前正在开发一种可安装的发动机.在发动机内我有以下两种型号:

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...