HABTM reference tables do not accept isolate_namespace in the mounted engine

I am currently developing a mounted engine. Inside the engine, I have the following two models:

  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

These are the db migration files:

  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

The problem starts when I try to get related objects. For example, when I call @channel.get :categories, I get the following error:

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

As you can see, its isolate_namespace value is missing in the link table, as it should look for an association in the ems_categories_channelsnot tablecategories_channels

Someone had similar problems or did I miss something?

+5
source share
1 answer

You can explicitly specify the name of the connection table ( in 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
+5
source

All Articles