我有公司表和潜在客户表。我正在拉一堆线索,需要将每个线索与公司表中的2个ID列组合在一起。我希望将公司表保留为这些ID的唯一真实来源(因此不会将这些列添加到可能不同步的潜在客户表中)。
现在我拉出所有潜在客户并遍历所有人以添加此数据。我假设有一个更有效的方法来做到这一点,但我不知道如何。有人可以帮忙吗?
leads = @user.find_leads(start_date: start_date, end_date: end_date).leads('true').includes(:company).select(fields).limit(limit).offset(offset).order("created_at DESC")
leads.each do |l|
company = l.company.present? ? l.company.company_external_id : ""
distributor = l.company.present? ? l.company.mfg_distributor_id : ""
lead_data = l.attributes.merge({external_company_id: company, external_distributor_id: distributor})
final_lead = lead_data.each_with_object({}) { |(k,v),g| g[k] = (Hash === v) ? denilize(v) : v ? v : "" }
all_leads << final_lead.stringify_keys
end
答案 0 :(得分:0)
lead_data = @user.find_leads(start_date: start_date, end_date: end_date).leads('true')
.left_outer_joins(:company)
.select(fields)
.select("companies.company_external_id AS external_company_id, companies.mfg_distributor_id AS external_distributor_id")
.limit(limit).offset(offset).order("created_at DESC")
.map(&:attributes)
final_lead = ...
all_leads << ...