Matching roll_no then loop 3 times, next loop

Hi, I am beginner RoR. I need your advanced programming…

@rollresults = RollResult.where(:banker_id =>‘3’)

| banker_id | roll_no |

| 3 | 332 |

| 3 | 512 | <-- when roll_no match ‘512’

| 3 | 332 | -|

| 3 | 512 | > listing them 3 times

| 3 | 512 | -|

| 3 | 332 |

| 3 | 332 |

| 3 | 512 | <-- when roll_no match ‘512’

| 3 | 264 | -|

| 3 | 332 | > listing them 3 times

| 3 | 666 | -|

| 3 | 512 | <-- when roll_no match ‘512’

| 3 | 512 | > listing

The output I want as below:

| banker_id | roll_no |

| 3 | 332 |

| 3 | 512 |

| 3 | 512 |


| 3 | 264 |

| 3 | 332 |

| 3 | 666 |


| 3 | 512 |

Please help me…

On Jul 21, 9:43am, Joachim C. [email protected] wrote:

Hi, I am beginner RoR. I need your advanced programming…

@rollresults = RollResult.where(:banker_id =>‘3’)

So you want to group your results by roll_no ?

Fred

Frederick C. wrote in post #1012069:

On Jul 21, 9:43am, Joachim C. [email protected] wrote:

Hi, I am beginner RoR. I need your advanced programming…

@rollresults = RollResult.where(:banker_id =>‘3’)

So you want to group your results by roll_no ?

Fred

Yes, I want to result group by roll_no.

@rollresults = RollResult.where(:banker_id =>‘3’)
@rollresults.each do |p|
if p.roll_no == ‘512’ #<— Can’t work
# group by 3times…
else
# continues finding roll_no == ‘512’
end
end

Can you teach me…?

On Jul 21, 11:08am, Joachim C. [email protected] wrote:

I don’t understand what you mean by ‘group by 3 times’, but you might
want to look at group_by (method on array) or at sql’s “group by”
clause

Fred

Hi, everyone. I have solved this problem by myself.
Thank you for Frederick C. guide me…

Frederick C. wrote in post #1012126:

On Jul 21, 11:08am, Joachim C. [email protected] wrote:

I don’t understand what you mean by ‘group by 3 times’, but you might
want to look at group_by (method on array) or at sql’s “group by”
clause

Fred

Emm… ok, I just want to look the loop.
In the beginning, we searching the roll_no is equal to ‘512’.
Next collecting 3 steps, roll_no = [332, 512, 512], to output.

After that, we searching the roll_no also equal to ‘512’.
Next collecting 3steps again, roll_no = [264, 332, 666], to output.

Repeat the same method to find roll_no == ‘512’, next collect 3steps and
show them to output.

Can you make it? Thank you. :smiley: