재귀 parent_id 열이있는 모델 카테고리가 있습니다. DB 내부 sql_view (readonly) 조인 테이블을 만들었습니다 (recursive postgres 기능을 사용하면 db가 자동으로 모든 관계를 유지 관리하고 모든 관련 범주 쌍에 대해 ancestor_id & descendent_id를 "만듭니다").postgresql보기 테이블 조인 & has_and_belongs_to_many_association, 콜백을 건너 뛰는 방법
그와class Category < ActiveRecord::Base
belongs_to :parent,
class_name: 'Category',
foreign_key: :parent_id
has_and_belongs_to_many :category_ancestors,
class_name: 'Category',
join_table: 'category_ancestors',
foreign_key: :descendent_id,
association_foreign_key: :ancestor_id
has_and_belongs_to_many :category_descendents,
class_name: 'Category',
join_table: 'category_ancestors',
foreign_key: :ancestor_id,
association_foreign_key: :descendent_id
end
, 난 단지 PARENT_ID 절약으로
some_category.category_ancestors
some_another.category_descendents
를 사용할 수 있습니다.
카테고리를 제거한 후 ActiveRecord는 자동으로 category_ancestors 테이블에서 관련된 모든 레코드를 제거하려고 시도합니다. category_ancestors 테이블은 읽기 전용이며 예외를 throw합니다.
ActiveRecord에 join_table 레코드를 업데이트하지 말라고 말하는 방법은 없나요? 필자는 자동 저장을 시도했습니다 : false, 성공하지 못했습니다.