2012-03-21 94 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