2011-12-12 4 views
0

Ruby on Rails에 3 가지 모델이 있습니다. 게시자, 저자 및 도서가 있습니다. 나는 그 (것)들 사이에 많은 것에 많은 관계를 창조하고 싶다.레일 : has_many_and_belongs_to on 3 models

나는 authors_books_publishers라는 테이블을 만들고 거기에 has_many_and_belongs_to를 추가했습니다.

Author 
has_many_and_belongs_to :books 
has_many_and_belongs_to :publishers 

Book 
has_many_and_belongs_to :authors 
has_many_and_belongs_to : publishers 

Publisher 
has_many_and_belongs_to :authors 
has_many_and_belongs_to :books 

하지만 저자

a = Author.new 
b = Book.new 
a.books << b 

그것은 말한다 (그리고 당연히 그렇게) 해당 시스템 테이블에게 전화 authors_books를 찾을 수없는 책을 추가하려고하면 작동하지 않습니다.

도와주세요

P. 위의 모델을 다음과 같이 취급하십시오. 그들은 내 앱의 실제 이름이 아닙니다.

감사합니다,

+0

방금 ​​같은 질문에 답했습니다. http://stackoverflow.com/questions/8375131/custom-db-entry-for-3-way-habtm-in-ror/8418075#8418075 – miaout17

+0

아마도 'has_and_belongs_to_many '. 나는 이런 종류의 'has_many_and_belongs_to'연관을 모른다. – megas

+0

http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many에 따르면 has_and_belongs_to_many를 사용하면 조인 테이블을 생성해야합니다. 따라서 귀하의 경우 authors_books 테이블 – Matilda

답변

1

당신이 3 개 별도의 테이블을 만들 수 있습니다

  1. authors_books
  2. authors_publishers

books_publishers와 당신 아마 authors_publishers 협회 중복 B/C 너 할 수있어. 같은 일을하는 것 author has_many :publishers, :thorough => :books