views:

43

answers:

1

I have a self join through an intersect table in an app I'm working on like below.

class User < ActiveRecord::Base
  has_many :clients, :through => :client_relationships, :conditions => "approved='1'", :source=>:user
end

It's working in the sense that I can say @current_user.clients and get all of the clients. However, I'd like to set up a URL /clients where I can list all of the current users clients. Does anyone know how I can do this?

I've tried setting up a :clients resource in my routes and a clients controller, but since there's no Clients model it throws and error.

A: 

There should be no error.

# routes.rb
map.resources :clients

# clients_controller.rb
class ClientsController < ApplicationController
  def index
    @clients = @current_user.clients
  end

  # other actions...
end  

# clients index.html.erb
<% @clients.each do |c| %>
  <p><%= c.name %></p>
<% end %>
klew
Ok, I was able to get it work. I'm using CanCan for role based authorization and had the method load_and_authorize_resource at the top of the controller which must assume that each controller has a model. One weird behaviour I'm getting thought is that in the index action I can access current_user, but not @current_user, even though in all my other controllers I user @current_user. Do you know why that might be?
John
Nevermind, I think I figured it out. The other controllers had a before_filter that was setting the @current_user variable.
John