Forum: Ruby on Rails HABTM query return all results

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
James W. (Guest)
on 2006-04-07 14:38
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]).


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

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

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

Rights Model:

class Right < ActiveRecord::Base
 has_and_belongs_to_many :roles

Roles Model:

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

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_roles.role_id = ?}, ], :joins => ', rights_roles' )

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"?
This topic is locked and can not be replied to.