2009-09-15 2 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 실제 테이블 이름에 그 이름을 별명을 할 수있는 방법이 및 프로젝트에 대해 반복 여전히 데이터베이스 (MySQL 또는 PostgreSQL)에서 CRUD 기능을 허용합니까?

[편집] 와우. 나는 충분한 커피를 갖기 전에 공개적으로 대답하고 질문하는 것을 멈춰야한다. hmt에서 habtm으로 주석 처리 된 부분을 변경했습니다. 시도한 다양한 옵션을 반영하여 코드 부분을 주석 처리했습니다. 그래서 당신은, 단순히 HABTM와 함께 사용할 조인 테이블의 이름을 지정해야하는 경우

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 

:

답변

6

나는 당신이이 모델을 함께 가입 같은 것을 가지고 있으리라 믿고있어.

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 속성에 대해 알지 못했습니다. 지난 몇 주간 인터넷 검색으로도. 당신의 도움을 주셔서 감사합니다! –