ruby on rails - has_many through help, what am I doing wrong? -
my models this:
user    has_and_belongs_to_many :roles  role    has_and_belongs_to_many :users tables:
roles_users    user_id    role_id  rolegroups    id    role_id    some_column now want create association on user model, collection of rolegroups user belongs to.
i.e. if user in roles id's 1 , 2, fetch rolegroups role_id = 1 , 2.
i think need use through because based on user's role association right?
i tried:
user    has_many  :rolegroups, :through => :roles  role    has_many :rolegroups, :through => :user but error saying:
activerecord::hasmanythroughsourceassociationmacroerror: invalid source reflection macro :has_many :through has_many :rolegroups, :through => :roles.  use :source specify source reflection. update ok models now:
user    habtm :roles    has_many :rolegroups, :through => :roles  role   habtm :users   has_many :rolegroups  rolegroup   belongs_to :role mysql tables:
roles_users    user_id    role_id  role_groups    id    role_id    col3    col4    .. if do:
u = user.find(1) u.roles  (works fine) u.rolegroups   #see error error message:
activerecord::statementinvalid: mysql2::error: unknown column 'roles.user_id' in 'where clause': select `role_groups`.* `role_groups` inner join `roles` on `role_groups`.role_id = `roles`.id ((`roles`.user_id = 1)) 
you're looking has_and_belongs_to_many association.
Comments
Post a Comment