2017-06-14 97 views
1

關於如何爲這個軌道控制器建立一個更好的循環的任何想法?寫這個循環的更好方法是什麼?

有時我在邏輯循環中有點迷路,所以我訴諸於尼安德特人的代碼。

def index 

@step1_status = current_user.steps.pluck(:step1).first 
@step2_status = current_user.steps.pluck(:step2).first 
@step3_status = current_user.steps.pluck(:step3).first 
@step4_status = current_user.steps.pluck(:step4).first 
@step5_status = current_user.steps.pluck(:step5).first 
@step6_status = current_user.steps.pluck(:step6).first 
@step7_status = current_user.steps.pluck(:step7).first 
@step8_status = current_user.steps.pluck(:step8).first 
@step9_status = current_user.steps.pluck(:step9).first 

if @step9_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2', '3', '4', '5', '6', '7', '8', '9']) 
elsif @step8_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2', '3', '4', '5', '6', '7', '8']) 
elsif @step7_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2', '3', '4', '5', '6', '7']) 
elsif @step6_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2', '3', '4', '5', '6']) 
elsif @step5_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2', '3', '4', '5']) 
elsif @step4_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2', '3', '4']) 
elsif @step3_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2', '3']) 
elsif @step2_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => ['1', '2']) 
elsif @step1_status == true 
    @task = Task.limit(1).order('sort_id ASC').where.not(:sort_id => '1') 
else 
    @task = Task.limit(1).order('sort_id ASC') 
end 

end 

感謝您的幫助!

+1

爲了從中獲取不同的單個列,拔9次同一行(9個DB調用!)有什麼意義?爲什麼你不把這個'stepN'怪物變成一個散亂的表(即規範化它),所以你可以實際使用步數作爲數據,而不是「訴諸於neandertal代碼」來擺弄列名?這是故意通過設計完成的,還是這是一個設計監督? –

+0

什麼循環?這裏沒有循環。 –

+0

哈哈,我知道。這是狗屎。 – Jordan

回答

1

我認爲這應該是一樣的。

首先嚐試

@step_status = [] 
9.times do |n| 
    @step_status << current_user.steps.pluck("step#{n+1}").first 
end 

highest_step = @step_status.rindex(true) 
if highest_step 
    @task = Task.order('sort_id ASC').where.not(sort_id: (1..(highest_step+1))).first 
else 
    @task = Task.order('sort_id ASC').first 
end 

有點更加優化,更少的代碼

highest_step = (1..9).to_a.rindex{|n| current_user.steps.pluck("step#{n+1}").first} + 1 

@task = Task.order('sort_id ASC') 
@task = @task.where.not(sort_id: (1..(highest_step))) if highest_step 
@task = @task.first 

只有2 DB調用

pluck_array = (1..9).map{|n| "step#{n}"} 
highest_step = current_user.steps.pluck(pluck_array).first.rindex(true) + 1 
#the + 1 accounts for the 1 being at index 0 through 9 at index 8 

@task = Task.order('sort_id ASC') 
@task = @task.where.not(sort_id: (1..(highest_step))) if highest_step 
@task = @task.first 

有點更容易閱讀(雖然有點長)

step_array = (1..9).to_a 
pluck_array = step_array.map{|n| "step#{n}"} 
highest_index = current_user.steps.pluck(pluck_array).first.rindex(true) 
highest_step = step_array[highest_index] 

@task = Task.order('sort_id ASC') 
@task = @task.where.not(sort_id: (1..(highest_step))) if highest_step 
@task = @task.first 
+0

我想我可以變小... –

+2

這個編輯不僅使代碼最小化,而且在數據庫最高真值之後擊中數據庫 –

+0

考慮一下,你可以在1個DB調用中完成第一個塊的操作 –

相關問題