1

내가 오류를 수신하고 또 다른 has_many와 소스를 통해 has_many에 대한 알 수없는 속성을 쓸 수 없습니다 다음 명령으로 계정에 목록을 추가하십시오.은 같은 테이블

a = Account.create(email: 'email', password: 'secret', password_confirmation: 'secret') 
list1 = List.create(creator: a, list_type: music, name: "a's list 1") 
a.subscriptions << list1 

대표 이름을 변경했습니다. account.lists 응용 프로그램의 특성으로 인해 다 대다 조인이 필요합니다. 계정에는 등록 된 많은 목록뿐만 아니라 작성자의 많은 목록이있을 수 있습니다.

나는 오류가 모호하게되는 방식에서, 그것은 Accountsubscriptions 수집 상 List의 추가를 위해 사용할 수있는 권리 관계를 찾는 데 문제가 있다고 생각합니다.

아이디어가 있으십니까?

답변

1

그래서, 모든 문서를 읽고 나의 필요와 has_many :throughhas_and_belongs_to_many의 구성 옵션보고 후, 나는 정말 그렇지 않은 has_many :through을 필요로 인해 조인의 특성, 그리고 그 실현 has_and_belongs_to_many 이후 (짧게는 habtm)은 내가 정의 할 필요가있는 모든 구성 옵션을 가지고 있습니다. 정확히이되어야합니다. 나는 그 선택을 시험해보기로 결정했다. 아래는 동일한 문제가있는 미래의 Google 직원을위한 작업 코드입니다.

class List < ActiveRecord::Base 
    has_and_belongs_to_many :parents, -> {uniq}, class_name: 'List', join_table: 'lists_lists', foreign_key: 'parent_id', association_foreign_key: 'child_id' 
    has_and_belongs_to_many :children, -> {uniq}, class_name: 'List', join_table: 'lists_lists', foreign_key: 'child_id', association_foreign_key: 'parent_id' 
end 

이것은 많은 - 투 - 많은 ListLists을 가질 수있는 기본적으로 어떤 차례로 Lists을 가질 수 있습니다 :

class Account < ActiveRecord::Base 
    has_and_belongs_to_many :subscriptions, -> {uniq}, class_name: 'List', join_table: 'accounts_lists', foreign_key: 'subscription_id', association_foreign_key: 'subscriber_id' 
    has_many :lists, foreign_key: 'creator_id' 
end 

class List < ActiveRecord::Base 
    has_and_belongs_to_many :subscribers, -> {uniq}, class_name: 'Account', join_table: 'accounts_lists', foreign_key: 'subscriber_id', association_foreign_key: 'subscription_id' 
    belongs_to :creator, class_name: 'Account', foreign_key: 'creator_id' 
end 

그리고 단지 내가 가진 또 다른 흥미로운 가입 시나리오를 지적 할 수는 다음을이었다 Lists 기타 등등을 가질 수 있습니다 ...

앞으로이 문제로 다른 사람들에게 도움이되기를 희망합니다 ...하지만 .... 누구든지 has_many :through 그런 결합 유형을 사용하여 이것이 가능한지 알고 싶습니다.