내 모델은 다음과 같습니다has_many 도움을 통해, 내가 뭘 잘못하고있는거야?
User
has_and_belongs_to_many :Roles
Role
has_and_belongs_to_many :Users
테이블 :
roles_users
user_id
role_id
roleGroups
id
role_id
some_column
가 지금은 사용자가 속한 모든 roleGroups의 수집 할 사용자 모델에 다른 연결을 만들려고합니다.
즉, 사용자는 ID의 1, 2, 다음 ROLE_ID = 1, 2
나는 그것이 사용자의 역할을 기반으로하기 때문에 내가 통해를 사용할 필요가 있다고 생각하는 모든 RoleGroups를 가져와 역할에있는 경우 협회 맞지?
User
has_many :RoleGroups, :through => :Roles
Role
has_many :RoleGroups, :through => :User
하지만 내가 말하는 오류를 얻을 :
나는 시도
ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_many :through for has_many :RoleGroups, :through => :Roles. Use :source to specify the source reflection.
업데이트 확인 나의 모델은 지금과 같이 :
User
habtm :Roles
has_many :RoleGroups, :through => :Roles
Role
habtm :Users
has_many :RoleGroups
RoleGroup
belongs_to :Role
MySQL의 테이블 :
roles_users
user_id
role_id
role_groups
id
role_id
col3
col4
..
내가 할 경우
u = User.find(1)
u.Roles (works fine)
u.RoleGroups #see error
오류 메시지 : 당신은 has_and_belongs_to_many
협회 찾고있는
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'roles.user_id' in 'where clause': SELECT `role_groups`.* FROM `role_groups` INNER JOIN `roles` ON `role_groups`.role_id = `roles`.id WHERE ((`roles`.user_id = 1))
사용자 + RoleGroup과 Role + RoleGroup 사이? 하지만 조인 테이블 RoleGroups에는 사용자 ID가 없습니다 ... – Blankman
'has_and_belongs_to_many'가있는 조인 테이블의 모델은 필요하지 않습니다.링크 된 레일 가이드를 보면 테이블 레이아웃을 보여주는 다이어그램이 있습니다. –
안녕하세요, 당신 타입이 맞았습니다. 하지만 여전히 작동하지 않습니다. user.RoleGroups, 질문에 오류를 넣습니다. – Blankman