HABTM query return all results


#1

I have a role-based authorization system exactly like the one from Rails
Recepies. I am looking to return all rights however in my view I want to
highlight the rights that are associated with the currently selected
role (passed in through @params[:id]).

Schema:

create_table “rights”, :force => true do |t|
t.column “name”, :string
t.column “controller”, :string
t.column “action”, :string
end

create_table “rights_roles”, :id => false, :force => true do |t|
t.column “right_id”, :integer
t.column “role_id”, :integer
end

create_table “roles”, :force => true do |t|
t.column “name”, :string
t.column “description”, :text
end

Rights Model:

class Right < ActiveRecord::Base
has_and_belongs_to_many :roles
end

Roles Model:

class Role < ActiveRecord::Base
has_and_belongs_to_many :employees
has_and_belongs_to_many :rights
end

I have tried and using the following I can get a list of all the rights
associated with the current role (I think there must be an easier way,
The AWDWR book does not explain this type of query):

def view_role_rights
role = Role.find_by_id(1)
@rights = Right.find(:all, :conditions => [%{rights.id =
rights_roles.role_id = ?},role.id ], :joins => ‘, rights_roles’ )
end

I want to be able to say “get me all the rights, highlight to the rights
that are associated with the role with the id of 1”?