2014-07-23 19 views
0

我有一个名为@jdc_array的实例变量,对检查的内容是这样的:采用注射法,总结数组的情况下轨

"[#<ActiveRecord::Associations::CollectionProxy [#<JobDeliveryCost id: 13, job_id: 53, delivery_cost_id: 1, cost_per_unit: 50.0, quantity: 3, timing: \"install\", created_at: \"2014-07-23 15:20:34\", updated_at: \"2014-07-23 15:20:34\">, #<JobDeliveryCost id: 15, job_id: 53, delivery_cost_id: 1, cost_per_unit: 50.0, quantity: 5, timing: \"install\", created_at: \"2014-07-23 15:57:45\", updated_at: \"2014-07-23 15:57:45\">, #<JobDeliveryCost id: 18, job_id: 53, delivery_cost_id: 1, cost_per_unit: 44.0, quantity: 1, timing: \"breakdown\", created_at: \"2014-07-23 18:27:20\", updated_at: \"2014-07-23 18:27:20\">, #<JobDeliveryCost id: 19, job_id: 53, delivery_cost_id: 1, cost_per_unit: 22.0, quantity: 1, timing: \"install\", created_at: \"2014-07-23 18:27:28\", updated_at: \"2014-07-23 18:27:28\">, #<JobDeliveryCost id: 20, job_id: 53, delivery_cost_id: 1, cost_per_unit: 3.0, quantity: 1, timing: \"install\", created_at: \"2014-07-23 18:28:45\", updated_at: \"2014-07-23 18:28:45\">]>, nil]" 

我要总结的cost_per_unit:所有的情况下,所以我创造了这个方法

def calculate_delivery_total(array) 
    array.map(&:cost_per_unit).inject(0, &:+) 
    end 

并调用像这样的方法:

def index 
    if get_deliverable 
     @jdc_array=(@job.job_delivery_costs.any? ? [@job.job_delivery_costs,@new_delivery] : [@new_delivery]) 
     @new_delivery = @deliverable.job_delivery_costs.build 
    end 
    set_job_delivery_cost 
    @total = calculate_delivery_total(@jdc_array) 
    end 

但是,我发现这个错误!!!

formal argument cannot be an instance variable def calculate_delivery_total(@array) 

三个问题。为什么我得到这个错误,我该如何解决它?第三,在控制器中做这样的事情是好的形式,还是我应该在别处做,以及如何做?

UPDATE

所以下面SO海报的建议,我改变了方法,这

def calculate_delivery_total(array) 
    array.map(&:cost_per_unit).inject(0, &:+) 
    end 

,但我仍然得到这个错误

​​

我可以在控制台做到这一点,为什么不会它在这里工作?!

+1

该错误是哪一行? – tadman

+0

这个错误是一个语法错误 - 它与'@ jdc_array'的_value_没有任何关系 - 你可能会忘记代码中的某处有'end'。 –

+0

@UriAgassi你现在可以检查吗? – user3868832

回答

0

@jdc_array可以是两个值中的一个:

[@job.job_delivery_costs,@new_delivery] 

或者

[@new_delivery] 

我不知道什么是@new_delivery,但job_delivery_costs返回coll(更具体地说是CollectionProxy)的JobDeliveryCost对象,这意味着您在集合中有一个集合。

在另一方面,@new_delivery可能nil(根据您的样本数据在文章的开头),可能是因为你值赋给它你已经把它的阵列(数值在后数组不会改变) - 你需要交换这些行。

我的建议是要改变接收两个参数的方法 - 的快递费用清单,以及新的交付,这样的事情:

def calculate_delivery_total(deliveries, new_delivery) 
    total = 0 
    unless deliveries.nil? 
    total = deliveries.map(&:cost_per_unit) 
    end 
    unless new_delivery.nil? 
    total += new_delivery.cost_per_unit 
    end 
    total 
end 

和你index代码也许应该是这样的:

def index 
    if get_deliverable 
     @new_delivery = @deliverable.job_delivery_costs.build 
    end 
    set_job_delivery_cost 
    @total = calculate_delivery_total(@job.job_delivery_costs,@new_delivery) 
    end 
0

要回答你的第二个问题,我使用一个门面模式为视图准备数据。这避免了视图或控制器中的计算,并促进了关注和重用的分离。

这是一个资源,但我可能会在编辑中进行扩展。

https://medium.com/@ryakh/facade-pattern-on-rails-d65b86cdb5b1


有关错误,这是一个猜测,但会不会是数组的最后一个元素是零?你是否尝试过.to_a和compact?

def calculate_delivery_total(array) array.to_a.compact.map(&:cost_per_unit).inject(0, &:+) end

+0

这很容易混淆,因为错误应该是'未定义的方法为零',而不是语法错误。 如果这没有帮助,它将有助于提供rails和ruby版本。 也是返回您的@jdc_array的代码片段。 –

+0

我的坏@AFaderDarkly,我有一个尾随*。但仍然这不起作用 def calculate_delivery_total(@array) @ array.map(&:cost_per_unit).inject(0,&:+) end – user3868832

+0

感谢您的文章的方式,请参阅您使用的更新 – user3868832

相关问题