2011-12-21 3 views
4

내가 서류와 접촉 사이의 많은 to_many 관계 없다 준다 :has_and_belongs_to_many 따라서 그들이 has_and_belongs_to_many 관계가 "이러한 테이블"오류

class Dossier < ActiveRecord::Base 
    has_and_belongs_to_many :contacts 

및 문건 제어기 표시 방법에

class Contact < ActiveRecord::Base 
    has_and_belongs_to_many :dossiers 

나는 이것을 가지고있다 :

@dossier = current_user.company.dossiers.find(params[:id])  
@dossier_contacts = @dossier.contacts 

그러나 쇼보기를 요청하면, 나는 등 오류가 : 테이블이 존재

<li><%= I18n.t :dossier_nr_contacts %></li><li><%= @dossier_contacts.count.to_s %></li> 

나는 관계가 올바른 설정 한 생각하지만, 그것은 오류를 제공 왜 지금은 안 :

SQLite3::SQLException: no such table: contacts_dossiers: SELECT COUNT(*) FROM "contacts" INNER JOIN "contacts_dossiers" ON "contacts"."id" = "contacts_dossiers"."contact_id" WHERE "contacts_dossiers"."dossier_id" = 1 

뷰는 다음과 같습니다. 모든 단서?

편집 : 내가 한 마이그레이션 :

class CreateDossiersContactsJoinTable < ActiveRecord::Migration 
    def up 
    create_table :dossiers_contacts, :id => false do |t| 
     t.integer :dossier_id 
     t.integer :contact_id 
    end 
    end 

    def self.down 
    drop_table :dossiers_contacts 
    end 
end 
+0

당신은 어떻게 테이블이 존재 알고 :

contacts_dossiers (기본적으로 알파벳 순서)

If you create a has_and_belongs_to_many association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the :join_table option, Active Record creates the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of “customers_orders” because “c” outranks “o” in lexical ordering.

는 소스가되어야 하는가? SQLite3은 당신과 동의하지 않는 것 같습니다. 예를 들어,'rake db : schema : dump'를 다시 실행 한 다음'db/schema.rb'를 살펴보십시오. 귀하의 레일 물건은 여기에 문제가되지 않습니다. – Peter

+0

'has_and_belong_to_many' 연관에 3 개의 테이블이 관련되어 있습니다. 링크 테이블이 누락되었습니다. 'contact_id'와'dossier_id'라는 두 개의 열과 각 열의 인덱스가있는 'contacts_dossiers'라는 새 마이그레이션이 db : migrate를 실행합니다. habtm에 대한 추가 정보 http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association (사진보기 => 3 개 테이블) – cristian

+0

나는 (위의 편집 된 포스트를 참조하십시오) 마이 그 레이션을 실행했는데 테이블이 존재합니다. 데이터베이스, 나는 이미 그것을 확인했다. – John

답변

1

당신이 has_and_belongs_to_many 당신 alsways를 사용하는 경우 당신을 위해 ID 쌍을 유지하는 연관 테이블이 필요합니다. 규칙에 따라이 테이블은 두 테이블이 결합 된 것처럼 불려집니다! 귀하의 예에서는 contacts_dossiers입니다.

이 표를 만들어야합니다. 마이그레이션을 만들고 다음과 같은 테이블을 만듭니다.

create_table :contacts_dossiers, :id => false do |f| 
    f.integer :contact_id, :dossier_id 
end 

그런 다음 테이블을 마이그레이션하면 모든 것이 작동합니다!

+0

위의 편집 된 게시물보기, 표가 존재합니다. – John

+0

SQLite Concole을 사용하여 db에 연결하려고 시도 했습니까? 테이블이 실제로 존재하는지 확인 했습니까? – davidb

+0

그래, 나는 심지어 수동으로 레코드를 추가했습니다. – John

관련 문제