I am trying to setup the has_many :through scheme but am having some difficulties. The long story short I am building a simple ACL. I have Users, Roles and Permissions. Below is what I started with for associations. class User < ActiveRecord::Base has_and_belongs_to_many :roles end class Role < ActiveRecord::Base has_and_belongs_to_many :users has_and_belongs_to_many :permissions end class Permission < ActiveRecord::Base has_and_belongs_to_many :roles end With this setup, everything was connected together and working fine. I would like to setup users having permissions through roles. I tried: class User < ActiveRecord::Base has_and_belongs_to_many :roles has_many :permissions, :through => :roles end but that does not work. From what I have read, if I can get the through to work, I should beable to do something like @user.permissions. How can I set this up so a user can have many roles, a role can have many permissions, a role can have many users, and a user can have permissions through roles? Any suggestions would be very helpful. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
has_many :through only works though another has_many, not a habtm. In order get a list of a users permissions, you''ll need to loop through their roles and collect all the permissions. Unless you''re planning on multiple installs of your app each with different permission requirements, you probably don''t actually need to store permissions in the DB. If that''s the case, you might want to check out the GateKeeper plugin ( http://gatekeeper.rubyforge.org/ ). jm wrote:> I am trying to setup the has_many :through scheme but am having some > difficulties. > > The long story short I am building a simple ACL. > I have Users, Roles and Permissions. Below is what I started with for > associations. > > class User < ActiveRecord::Base > has_and_belongs_to_many :roles > end > > class Role < ActiveRecord::Base > has_and_belongs_to_many :users > has_and_belongs_to_many :permissions > end > > class Permission < ActiveRecord::Base > has_and_belongs_to_many :roles > end > > With this setup, everything was connected together and working fine. > I would like to setup users having permissions through roles. > > I tried: > class User < ActiveRecord::Base > has_and_belongs_to_many :roles > has_many :permissions, :through => :roles > end > > but that does not work. From what I have read, if I can get the > through to work, I should beable to do something like > @user.permissions. > > How can I set this up so a user can have many roles, a role can have > many permissions, a role can have many users, and a user can have > permissions through roles? > > Any suggestions would be very helpful. > > > > >-- http://www.5valleys.com/ http://www.workingwithrails.com/person/8078 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Should I use this instead? class User < ActiveRecord::Base has_many :roles has_many :permissions, :through => :roles end class Permission < ActiveRecord::Base has_many :roles has_many :users, :through => :roles end class Role < ActiveRecord::Base belongs_to :user belongs_to :permission end On May 7, 9:49 am, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> has_many :through only works though another has_many, not a habtm. In > order get a list of a users permissions, you''ll need to loop through > their roles and collect all the permissions. > > Unless you''re planning on multiple installs of your app each with > different permission requirements, you probably don''t actually need to > store permissions in the DB. If that''s the case, you might want to check > out the GateKeeper plugin (http://gatekeeper.rubyforge.org/). > > > > jm wrote: > > I am trying to setup the has_many :through scheme but am having some > > difficulties. > > > The long story short I am building a simple ACL. > > I have Users, Roles and Permissions. Below is what I started with for > > associations. > > > class User < ActiveRecord::Base > > has_and_belongs_to_many :roles > > end > > > class Role < ActiveRecord::Base > > has_and_belongs_to_many :users > > has_and_belongs_to_many :permissions > > end > > > class Permission < ActiveRecord::Base > > has_and_belongs_to_many :roles > > end > > > With this setup, everything was connected together and working fine. > > I would like to setup users having permissions through roles. > > > I tried: > > class User < ActiveRecord::Base > > has_and_belongs_to_many :roles > > has_many :permissions, :through => :roles > > end > > > but that does not work. From what I have read, if I can get the > > through to work, I should beable to do something like > > @user.permissions. > > > How can I set this up so a user can have many roles, a role can have > > many permissions, a role can have many users, and a user can have > > permissions through roles? > > > Any suggestions would be very helpful. > > -- > > http://www.5valleys.com/ > > http://www.workingwithrails.com/person/8078--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---