关于如何为这个轨道控制器建立一个更好的循环的任何想法?写这个循环的更好方法是什么?
有时我在逻辑循环中有点迷路,所以我诉诸于尼安德特人的代码。
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
感谢您的帮助!
为了从中获取不同的单个列,拔9次同一行(9个DB调用!)有什么意义?为什么你不把这个'stepN'怪物变成一个散乱的表(即规范化它),所以你可以实际使用步数作为数据,而不是“诉诸于neandertal代码”来摆弄列名?这是故意通过设计完成的,还是这是一个设计监督? –
什么循环?这里没有循环。 –
哈哈,我知道。这是狗屎。 – Jordan