두 가지 모델 User 및 Category가 있습니다. 다음 코드를HABTM의 조인 테이블에 특성을 추가하고 연결 값을 가져올 때이를 가져 오는 방법은 무엇입니까?
class User < ActiveRecord::Base
has_and_belongs_to_many :categories
accepts_nested_attributes_for :categories, :allow_destroy => true
alias_method :categories=, :categories_attributes=
end
이를 이미 카테고리 테이블에 존재하지 않을 때 나는 카테고리를 만들
class Category < ActiveRecord::Base
has_and_belongs_to_many :users
end
을 고려하십시오. 카테고리가 테이블에 이미있는 경우 카테고리의 ID를 조인 테이블의 사용자로 참조해야합니다. 그리고 참조 유형과 함께 조인 테이블에 필드 say type을 추가해야 할 필요가 있다고 생각합니다.
말 예를 들어
user table:
1, sample_user
2, test_user
및
category table:
1, category1
2, category2
및
categories_users:
category_id user_id type
1 1 type1
2 1 type2
1 2 type2
1 2 type1
그리고 사용자의 범주를 가져올 때 난의 종류에 따라 분류 개체를 얻을 필요 category 객체 내의 category.
어떻게하면됩니까? 제발 도와주세요
가능한 중복 http://stackoverflow.com/questions/18575039/ :이 경우
, 그 같은 것을 얻을 것이다 how-can-i-use-accepts-nested-attributes-with-habtm) – gabrielhilal