# random access to my data

**URL:** <https://discuss.rubyonrails.org/t/random-access-to-my-data/7741>\
**Category:** rubyonrails-talk\
**Created:** [January 24, 2007, 7:36pm UTC](https://discuss.rubyonrails.org/t/random-access-to-my-data/7741 "2007-01-24T19:36:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![punkracy](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@punkracy](https://discuss.rubyonrails.org/u/punkracy)\
**Post date:** [January 24, 2007, 7:36pm UTC](https://discuss.rubyonrails.org/t/random-access-to-my-data/7741/1 "2007-01-24T19:36:15Z")

</div>

Hi, I have an algorithmic problem, and also problem with writing my idea's in Ruby Way.

I have model Section which keeps info about sections like (history, mathematic, biology etc.). I also have model with Question for questions appropriate to one category. I set correct relations between them (has\_many, belongs\_to), so I can get questions from first by: Section.find(:first).questions (I know it's an array\*)

code:

\<% for section in @sections %\> &nbsp;&nbsp;Section: \<%= section.name %\> &nbsp;&nbsp;\<% for question in section.questions %\> &nbsp;&nbsp;&nbsp;&nbsp;Question: \<%= question.id %\> - \<%= question.body %\> &nbsp;&nbsp;\<% end %\> \<% end %\>

will give me something like: Section: History Question: 2 - Lorem ipsum a Question: 3 - Lorem ipsum b Question: 7 - Lorem ipsum c Question: 12 - Lorem ipsum d

Section: Biology Question: 4 - Lorem ipsum x Question: 6 - Lorem ipsum y Question: 8 - Lorem ipsum z Question: 9 - Lorem ipsum w

Now I want to generate collection of random and unique questions from every category. Something like: Question: 3 - Lorem ipsum b Question: 7 - Lorem ipsum c Question: 4 - Lorem ipsum x Question: 9 - Lorem ipsum w

\*Using array, it would be something like (but should I do it this way?): a = Array.new(10) b = Array.new(7) a.fill {|i| i} r1 = 0..6 r1.each do |j| &nbsp;&nbsp;i = rand(10-j) &nbsp;&nbsp;x = a[i] &nbsp;&nbsp;b[j] = x &nbsp;&nbsp;a.delete\_at(i) end

Please give me some advice to write it with Rails in Ruby way 🙂

Thanks in advance
