2017-06-14 16 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 code "を使うのではなく、これは設計によって意図的に行われているのですか、これは設計上の見落としですか? –

+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コールで最初のブロック全体を行うことができる –

関連する問題