# Select box for multiple models

**URL:** <https://www.ruby-forum.com/t/select-box-for-multiple-models/143290>\
**Category:** Rails\
**Created:** [July 17, 2008, 12:32am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290 "2008-07-17T00:32:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 12:32am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/1 "2008-07-17T00:32:15Z")

</div>

Hi, guys.

I’m new to Rails and right now I’m trying to write simple test system.  
The thing is that I don’t know how get data from multiple tables and  
put the into one select box. Here is my db scheme and what I want to do.  
 → [http://mitkokostov.info/images/screenshot.jpg](http://mitkokostov.info/images/screenshot.jpg)

Thanks in advance,  
Mitko K.

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 12:47am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/2 "2008-07-17T00:47:29Z")

</div>

On Thu, 2008-07-17 at 00:32 +0200, Mitko K. wrote:

> Hi, guys.
> 
> I’m new to Rails and right now I’m trying to write simple test system.  
> The thing is that I don’t know how get data from multiple tables and  
> put the into one select box. Here is my db scheme and what I want to do.  
> → [http://mitkokostov.info/images/screenshot.jpg](http://mitkokostov.info/images/screenshot.jpg)

* * *

get a better concept first…

Class Question has\_many :answers  
:id integer primary\_key  
:answer\_id integer, foreign\_key # this is the correct answer id  
:question :string  
Class Answer belongs\_to :question  
:id integer primary\_key  
:question\_id integer  
:answer :string

@answers = Answer.find(:all, :conditions =\> [“question\_id = ?”,  
@question])

\<%= options = [[‘Select Answer’, ‘’]] + @answers.sort { |a,b| a.answer  
\<=\> b.answer }.collect {  
|ans| [ans.answer, ans.id] }  
select ‘quiz’, ‘answer\_id’, options %\>

Craig

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 12:54am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/3 "2008-07-17T00:54:03Z")

</div>

> @answers = Answer.find(:all, :conditions =\> [“question\_id = ?”,  
> @question])
> 
> \<%= options = [[‘Select Answer’, ‘’]] + @answers.sort { |a,b| a.answer  
> \<=\> b.answer }.collect {  
> |ans| [ans.answer, ans.id] }  
> select ‘quiz’, ‘answer\_id’, options %\>
> 
> Craig

Thanks very much, but I want to have suggestions, not only questions and  
answers.

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 1:08am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/4 "2008-07-17T01:08:48Z")

</div>

Craig W. wrote:

> On Thu, 2008-07-17 at 00:54 +0200, Mitko K. wrote:
> 
> > Thanks very much, but I want to have suggestions, not only questions and  
> > answers.
> 
> * * *
> 
> either I don’t understand your question or you didn’t understand my  
> answer. The answers file would provide multiple choices for each  
> question but only 1 correct answer for each question.
> 
> Craig

for example I have one question, answer to that question and multiple  
suggestions, but incorrect. The questions in my DB will be very  
different, so it will be inappropriate to use other answers as  
suggestions to a question.

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 1:16am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/5 "2008-07-17T01:16:28Z")

</div>

On Thu, 2008-07-17 at 01:08 +0200, Mitko K. wrote:

> > Craig
> 
> for example I have one question, answer to that question and multiple  
> suggestions, but incorrect. The questions in my DB will be very  
> different, so it will be inappropriate to use other answers as  
> suggestions to a question.

* * *

re-read my initial reply - it is the best solution I think.

Craig

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 1:01am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/6 "2008-07-17T01:01:01Z")

</div>

On Thu, 2008-07-17 at 00:54 +0200, Mitko K. wrote:

> Thanks very much, but I want to have suggestions, not only questions and  
> answers.

* * *

either I don’t understand your question or you didn’t understand my  
answer. The answers file would provide multiple choices for each  
question but only 1 correct answer for each question.

Craig

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 1:26am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/7 "2008-07-17T01:26:55Z")

</div>

> re-read my initial reply - it is the best solution I think.
> 
> Craig  
> I read it like four times.

For example I have:

1. What is the fastest car ? - Answer -\> Bugatti
2. How many minutes are in one hour -\> 60
3. What is the first grafical browser -\> Mosaic

With your approich when I get question number 1. What is the fastest car  
? the answers in the select box will be Bugatti, 60 and Mosaic and it  
will be obvious that the correct answer is Bugatti. Because of this I  
add third table which will contain similar, but incorrect answers for  
every question and I named it suggestions. So my purpose it to get  
question and render its correct answer and some “suggestions” ( through  
question\_id ).

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 1:35am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/8 "2008-07-17T01:35:47Z")

</div>

On Thu, 2008-07-17 at 01:26 +0200, Mitko K. wrote:

> With your approich when I get question number 1. What is the fastest car  
> ? the answers in the select box will be Bugatti, 60 and Mosaic and it  
> will be obvious that the correct answer is Bugatti. Because of this I  
> add third table which will contain similar, but incorrect answers for  
> every question and I named it suggestions. So my purpose it to get  
> question and render its correct answer and some “suggestions” ( through  
> question\_id ).

* * *

no - because if you look, I had ‘question\_id’ for each answer, so there  
could be as many/few answers associated with each specific question and  
only 1 correct answer for each question.

Craig

---

<div class="post-metadata">

**Author:** ![mitkok](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@mitkok](https://www.ruby-forum.com/u/mitkok)\
**Post date:** [July 17, 2008, 1:39am UTC](https://www.ruby-forum.com/t/select-box-for-multiple-models/143290/9 "2008-07-17T01:39:04Z")

</div>

Sorry, Craig.  
I’m so stupid. I really didn’t fully understand your way.

Thanks very much.
