Forum: Ruby on Rails HABTM query return all results

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-04-07 12: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]).

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