微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐