그래서, 모든 문서를 읽고 나의 필요와 has_many :through
및 has_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
이것은 많은 - 투 - 많은 List
이 Lists
을 가질 수있는 기본적으로 어떤 차례로 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
그런 결합 유형을 사용하여 이것이 가능한지 알고 싶습니다.