加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:21:38 所属栏目:百科 来源:网络整理
导读:我目前正在开发一种可安装的发动机.在发动机内我有以下两种型号: 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 这
我目前正在开发一种可安装的发动机.在发动机内我有以下两种型号:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读