2012-03-21 91 views
5

我目前正在开发可安装引擎。在发动机我有以下两种模式: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 

这些数据库迁移文件:

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_channelscategories_channels协会

任何人都有类似的问题,或者我错过了什么?

+0

我没有看到,您的引擎是否包含'isolate_namespace'本身? 模块埃姆斯 类引擎<滑轨::引擎 isolate_namespace埃姆斯 结束 结束 – 2012-03-24 10:37:25

回答

5

您可以明确设置连接表名称(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 
+0

是啊,我得到不过,我认为它应该自动就捡起。因为Rails在后台做得太多了,我不想要任何错误配置,因为他们可能会昏迷回去困扰我。 是否需要手动设置连接表的任何想法? – luxerama 2012-03-26 13:20:29

+1

通常,它使用两个表名称的约定,按字母顺序排序,使用任何设置模型'table_prefix'和'table_suffix'封装结果([如源代码中所述](https://github.com/rails/轨道/斑点/主/了activerecord/LIB/active_record /协会/助洗剂/ has_and_belongs_to_many.rb#L42))。 它似乎没有考虑到像模块为模型所做的连接表的“模块”封装。 如果你明确地设置它,那么你不需要使用':join_table'或潜在的模型前缀/后缀属性来做错配置。 – 2012-03-26 23:27:38