2014-07-23 26 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

相關問題