2016-07-01 4 views
0

HABTM 관계의 모델은 MaintenanceOrderMaintenance입니다.HABTM의 명명 규칙 문제

I have two models that are similar, and I'm wondering if this is the cause of the error? 

@maintenance.maintenances 
=> ActiveRecord::StatementInvalid: Could not find table 'maintenance_orders_maintenances' 
@maintenance_order.maintenances 
=> ActiveRecord::StatementInvalid: Could not find table 'maintenance_orders_maintenances' 

반면 :

create_table :maintenances_maintenance_orders, id: false do |t| 
    t.belongs_to :maintenance, index: true 
    t.belongs_to :maintenance_order, index: true 
end 

을 그럼 난 오류가 발생합니다 : 마이그레이션이 조인 테이블을 생성에 관해서

create_table :maintenance_orders do |t| 
    ... 
end 
create_table :maintenances do |t| 
    ... 
end 

다음과 같이 선언했다, 나는 경우 것으로 나타났습니다 오류가있는 경우 다음과 같은 조인 테이블을 제안합니다.

create_table :maintenance_orders_maintenances, id: false do |t| 
    t.belongs_to :maintenance_order, index: true 
    t.belongs_to :maintenance, index: true 
end 

모두 정상적으로 작동합니다. 그러나 HABTM의 정의에 따라 모델이 선언 된 순서가 중요하지 않기 때문에이 질문을 던지고 있습니다. 그래서 ... 이름이 너무 비슷해서 이런 식으로해야하는 뱀 사건이 있습니다. (아마도 maintenances_maintenance이 매우 혼란 스럽습니다) 아니면 ...? 이것에 대한 어떤 규칙?

+0

기본적으로 레일스는 테이블 이름을 알파벳순으로 정렬합니다. 'maintenance_order'는'maintenances' 앞에옵니다. 그래서 : : maintenance_orders_maintenances'는 레일스가 테이블 이름을 기대하는 것입니다 –

답변

1

기본적으로 레일스는 테이블 이름을 조인 테이블 이름의 알파벳 순서로 정렬합니다. maintenance_ordermaintenances 앞에 오므로 :maintenance_orders_maintenances은 테이블에 이름이 부여되는 레일입니다. 자세한 내용은 http://edgeguides.rubyonrails.org/active_record_migrations.html#creating-a-join-table을 참조하십시오.

레일스가 기대하는 것부터 출발하면 테이블 이름을 직접 구성 할 수 있지만 표준 명명 프로 시저를 따르는 것이 좋습니다.

모든 레일스 가이드를 읽어 보는 것이 가치있는 일임을 기억하십시오. 레일스 지식을 그렇게 잘 정돈 할 수 있습니다.