2009-09-15 54 views
3

這是一個單獨的問題,但它關係到一個較早的問題:Three Column Join in Rails with Active Scaffold。綜上所述:Rails的自動查找兩列連接表,但它並不適用於三列連接表做同樣的。我試着在前面的問題的建議,但它歸結爲:別名表名,以方便3列連接表(MySQL或PostgreSQL)

如果你有模型項目,員工,角色和每個具有其他兩個的habtm關係,軌將分別做各的關係,但不是整體。

class Employee < ActiveRecord::Base 
    #has_many  :employees_projects_roles 
    #has_many  :roles,  :through => :employees_projects_roles 
    #has_many  :projects, :through => :employees_projects_roles 
    has_and_belongs_to_many :roles 
    has_and_belongs_to_many :projects 
end 

重複項目,角色如下

class Role < ActiveRecord::Base 
    #has_many :employees, :through => :employees_projects_roles 
    #has_many :projects, :through => :employees_projects_roles 
    has_and_belongs_to_many :employees 
    has_and_belongs_to_many :projects 
end 

我的問題是這樣的,因爲軌道尋找employees_projects, projects_roles,並employees_roles但不employees_projects_roles是有沒有辦法別名這些名稱與實際表名和仍然允許數據庫中的CRUD功能(MySQL或PostgreSQL)?

[編輯] 糟糕。我必須停止回答和提問我已經受夠了咖啡公開之前。將註釋掉的部分從hmt更改爲habtm。包括註釋掉部分代碼以反映我嘗試過的各種選項。

回答

6

我假設你有這樣的事情您的加盟模式一起:

def self.up 
    create_table :my_join_table, :id => false do |t| 
     t.integer :employee_id 
     t.integer :role_id 
     t.integer :project_id 
     t.timestamps 
    end 
    end 

如果是這樣你,只需要指定與您的habtm使用join表的名稱。

class Employee < ActiveRecord::Base 
    has_and_belongs_to_many :roles, :join_table => "my_join_table" 
    has_and_belongs_to_many :projects, :join_table => "my_join_table" 
end 

class Project < ActiveRecord::Base 
    has_and_belongs_to_many :roles, :join_table => "my_join_table" 
    has_and_belongs_to_many :employees, :join_table => "my_join_table" 
end 

class Role < ActiveRecord::Base 
    has_and_belongs_to_many :employees, :join_table => "my_join_table" 
    has_and_belongs_to_many :projects, :join_table => "my_join_table" 
end 
+0

是的,連接表存在,但沒有我不知道:join_table屬性!即使在過去的幾周內使用Google搜索功能。謝謝你的幫助! – 2009-09-15 18:55:19