Ruby on rails 在循环中查找匹配项

Ruby on rails 在循环中查找匹配项,ruby-on-rails,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 5,我有一个循环,它获取用户输入的号码(无重复),并在此基础上插入某些信息 例如,如果用户输入数字3,则1和3之间的数字将插入r.U索引中 我要做的是为每个循环匹配每个唯一的数字,并插入treatment_index变量,这样对于每个r.repeation_索引,它不会重复相同的数字 例如,我的表格结果就是这样,不包括治疗指数 |id|treatment_selection_id|repetition_index|treatment_index| |1 | 1

我有一个循环,它获取用户输入的号码(无重复),并在此基础上插入某些信息

例如,如果用户输入数字3,则1和3之间的数字将插入r.U索引中

我要做的是为每个循环匹配每个唯一的数字,并插入treatment_index变量,这样对于每个r.repeation_索引,它不会重复相同的数字

例如,我的表格结果就是这样,不包括治疗指数

|id|treatment_selection_id|repetition_index|treatment_index|
|1 |         1            |       1        |               |
|2 |         1            |       2        |               |
|3 |         1            |       3        |               |
|4 |         2            |       1        |               |
|5 |         2            |       2        |               |
|6 |         2            |       3        |               |
|7 |         3            |       1        |               |
|8 |         3            |       2        |               |
|9 |         3            |       3        |               |
请注意,每个
治疗选择\u id
都有一个唯一的重复索引1..3。现在,对于每一个等于1的
repeation\u index
,我想在
treatment\u index
中插入一个唯一的数字1..3,以此类推,使用repeation\u index 2和3

到目前为止,我有以下内容,但是
r.treatment\u index
没有为每个匹配的
r.repeation\u index
插入唯一的编号

no_reps = @trial.number_of_repetitions
@trial.treatment_selections.each do |r|
 r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
   treatment_indexes = (1..no_reps).to_a.shuffle
   a.each_with_index do |r, j|
     r.repetition_index = j + 1
     r.treatment_index = treatment_indexes[j]
   end
  end
end

当下一个组被迭代时,将创建一个新的随机
treatment\u索引数组
,因此您将按照它们在
treatment\u selection\u id
级别中的分布顺序进行随机排序。在您的结果中,您应该看到相同编号的
treatment\u selection\u id
s将具有不同的
treatment\u index
。但是你找不到
repeation\u index
treatment\u index
之间的唯一性关系

no_reps = @trial.number_of_repetitions
@trial.treatment_selections.each do |r|
 r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
   treatment_indexes = (1..no_reps).to_a.shuffle
   a.each_with_index do |r, j|
     r.repetition_index = j + 1
     r.treatment_index = treatment_indexes[j]
   end
  end
end
您可以随机创建此
treatment\u索引
,但仍然需要搜索
repeation\u索引的外观
,并避免冲突。请注意,如果您不随机创建此索引,您可能会得到具有相同值的
treatment\u selection\u id
treatment\u index
,这将符合您要求的唯一性行为

此更改应提供随机性,并在
重复索引
级别中消除重复项

no_reps = @trial.number_of_repetitions
repet_treat = {} # you have to keep track of repetition_indexes and treatment_indexes
@trial.treatment_selections.each do |r|
 r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
   treatment_indexes = (1..no_reps).to_a # randomness will be handled elsewhere
   a.each_with_index do |r, j|
     r.repetition_index = j + 1
     # store treatment_index for this repetition_index in an array
     repeat_treat[r.repetition_index] ||= [] 
     # get the treatment_index you have already used for this repetition_index
     used_treat_indexes = repeat_treat[r.repetition_index]
     # delete the used indexes from the posibilities of the next and get a new random index
     r.treatment_index = (treatment_indexes - used_treat_indexes).sample
     # store your newely used treatment_index in its repetition_index group
     repeat_treat[r.repetition_index] << r.treatment_index
   end
  end
end

您是否在
@试用版中点击了save?因为在这段代码中,您只更新内存对象,而不更新数据库表对象。Yes@trial被保存。这是一个执行良好的解决方案。谢谢。还有一个问题。如果我有两个变量要考虑呢?例如r、 重复。在(3)的组中。每个组的索引都是do | a,i | treatment | index=(1..4)。如果treatment | index的长度依赖于循环之外的某个内容,则应该在“no | reps=”旁边初始化它,否则会重复创建相同的内容,正如您所看到的,该数组永远不会更改。如果它依赖于每个迭代中的某些内容,那么只要您没有用完处理索引,它就可以正常工作。如果您需要更多索引,请尝试从一开始就扩大数组,或者在已使用的最大索引中添加1。太好了。再次感谢你的帮助。让我摆脱困境。