Мои модели выглядят так:
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
Теперь я хочу создать еще одну ассоциацию для модели User, которая будет представлять собой совокупность всех групп ролейпользователь принадлежит.
т.е. если пользователь находится в ролях с идентификаторами 1 и 2, то извлекает все группы ролей, где role_id = 1 и 2.
Я думаю, что мне нужноиспользовать сквозной канал, потому что он основан на праве ассоциации ролей пользователя?
Я пытался:
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
Сообщение об ошибке:
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))