acl9 has been around for a while, but parts of it's code had bothered me for a while specially the use of
has_and_belongs_to_many, the problem was that this method did not let you have primary keys in your join table nor timestamps, etc.
Luckily rails 3.0 came out and changed the way associations work, here is how you can override your existing association and create a join model.
First create the following migrations:
class AddMissingFieldsToRolesUsers < ActiveRecord::Migration def change add_column :roles_users, :created_at, :datetime add_column :roles_users, :updated_at, :datetime add_column :roles_users, :id, :primary_key end end
Assuming your join table is named
roles_users and that it has no primary key nor timestamps.
class RenameRolesUsersToEnrollments < ActiveRecord::Migration def change rename_table :roles_users, :enrollments end end
We then rename our join table to
enrollments or whatever makes the most sense for you.
We can now create our join model and override
class Enrollment < ActiveRecord::Base belongs_to :user belongs_to :role validates :user, :role, :presence => true end class User < ActiveRecord::Base acts_as_authorization_subject :association_name => :roles, :join_table_name => :enrollments has_many :enrollments has_many :roles, :through => :enrollments end class Role < ActiveRecord::Base acts_as_authorization_role :join_table_name => :enrollments has_many :enrollments has_many :users, :through => :enrollments end
And there you have you can now use your join model to navigate either users or roles, you can add new fields to your join table, and add any kind of logic that your join model should need.
If you'd like to see a rails 3.2 application that is already using this checkout https://github.com/rubytij/usergroup the
acl9 methods such as
has_no_role! will still work.