我有两个模特客户和队友。
客户有项目。
每位队友都有自己的teammate_project
:
select(:client_project,current_user.clients.pluck(:project)
我需要在客户视图中显示哪些队友通过项目属于特定客户。但是在Clients / Teammates表中没有额外的DB列。
现在我有:
class FrontPagesController < ApplicationController
def front
if signed_in?
mess {current_user}
@clients.each do |client|
client_teammates = current_user.teammates.where(client_project: client.project)
@client_teammates = client_teammates.pluck(:secondname)
end
end
end
private
def mess
#multiple
@accounts = yield.accounts.all
@teammates = yield.teammates.all
@clients = yield.clients.all
@perks = yield.perks.all
#single
@account = yield.accounts.build
@teammate = yield.teammates.build
@client = yield.clients.build
@perk = yield.perks.build
end
end
并查看:
- @client_teammates.each do |c_t|
%ul
%li - #{c_t}
但它无法正常工作。这需要一些队友,但他们不正确
需要你的帮助
答案 0 :(得分:0)
我不知道你想要什么,但你的错误是显而易见的。
您应该将代码更改为:
def front
if signed_in?
mess {current_user}
@clients_teammates = []
@clients.each do |client|
client_teammates = current_user.teammates.where(client_project: client.project)
@clients_teammates << client_teammates.pluck(:secondname)
end
end
end
您可以通过以下方式改进代码:
def front
if signed_in?
mess {current_user}
@clients_teammates = current_user.teammates.where(client_project_id: @clients.pluck(:project_id))
# if @clients is an Array (if you use Rails3)
# @clients_teammates = current_user.teammates.where(client_project_id: @clients.map(&:project_id))
end
end
或者如果你是一个非常酷的程序员,你可以这样做:
def front
if signed_in?
mess {current_user}
@clients_teammates = current_user.teammates.joins("JOIN projects ON teammates.client_project_id = projects.id").joints("JOIN clients ON projects.id = clients.project_id").where("clients.user_id = ?", current_user.id)
end
end