# HABTM query return all results

**URL:** <https://www.ruby-forum.com/t/habtm-query-return-all-results/57134>\
**Category:** Rails\
**Created:** [April 7, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/habtm-query-return-all-results/57134 "2006-04-07T12:38:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [April 7, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/habtm-query-return-all-results/57134/1 "2006-04-07T12:38:37Z")

</div>

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”?
