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. 위의 모델을 다음과 같이 취급하십시오. 그들은 내 앱의 실제 이름이 아닙니다.
감사합니다,
방금 같은 질문에 답했습니다. http://stackoverflow.com/questions/8375131/custom-db-entry-for-3-way-habtm-in-ror/8418075#8418075 – miaout17
아마도 'has_and_belongs_to_many '. 나는 이런 종류의 'has_many_and_belongs_to'연관을 모른다. – megas
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