我通过我正在使用的应用程序中的相交表进行了 self 连接,如下所示。
class User < ActiveRecord::Base
has_many :clients, :through => :client_relationships, :conditions => "approved='1'", :source=>:user
end
它的工作原理是我可以说@current_user.clients 并获得所有客户。但是,我想设置一个 URL/clients,我可以在其中列出所有当前用户的客户端。有谁知道我该怎么做?
我已经尝试在我的路由和客户端 Controller 中设置一个 :clients 资源,但是由于没有客户端模型,它会抛出错误。
最佳答案
应该没有错误。
# 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 %>
关于ruby-on-rails - 基于自连接的 Ruby on Rails 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3314607/