has_many :through question

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.

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:

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