【发布时间】:2014-10-23 22:09:55
【问题描述】:
我有属于属于代理机构的公司的行动的线索行动。
我要做的是在代理级别获取属于该代理的公司的所有操作的值总和(在 Leadactions 表的列中定义)。
我有需要的两个数组,但我不知道如何正确总结。
型号 类 Leadaction
class Action < ActiveRecord::Base
belongs_to :company
has_one :leadaction
end
class Company < ActiveRecord::Base
belongs_to :agency
has_many :actions
belongs_to :agency
end
class Agency < ActiveRecord::Base
has_many :companies
has_many :actions, through: :companies
has_many :leadactions, through: :actions
end
控制器
class AgenciesController < ApplicationController
def show
@user = current_user
@agency = @user.agency
@companies = @agency.companies.all
@leads = @agency.actions.where.not(leadaction_id: '').map(&:leadaction_id.to_proc)
@leadvalues = Leadaction.all.map { |v| v.value }
end
@leads 给了我一个如下所示的数组: [1, 2, 1, 3, 1, 2, 2, 2, 3, 1, 1, 2, 2, 1, 2, 2]关联的leadaction_id 列。
@leadvalues 给我:[[1, 300], [2, 6000], [3, 300]] 这是 Leadaction.id 和 Leadaction.value
我不知道如何获取它,所以第一个数组中的任何地方都有 1 会显示 300,有 2 的地方会显示 6000,有 3 的地方会显示 300。所以我最终会得到一个看起来像这样的数组: [300、6000、300、300、300、6000、6000、6000、300、300、300、6000、6000、300、6000、6000]
有什么办法可以做到这一点吗?请帮助我的头脑在这 1 个问题上工作至少 12 个小时。
【问题讨论】:
标签: arrays ruby-on-rails-4 associations