# Multiple collection selects of same data types in view?

**URL:** <https://www.ruby-forum.com/t/multiple-collection-selects-of-same-data-types-in-view/172715>\
**Category:** Rails\
**Created:** [July 26, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/multiple-collection-selects-of-same-data-types-in-view/172715 "2009-07-26T17:39:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 26, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/multiple-collection-selects-of-same-data-types-in-view/172715/1 "2009-07-26T17:39:54Z")

</div>

Summary of what I’d like to accomplish:

I’m performing a side-by-side comparison of two table objects and their  
data housed in multiple tables. The model associations are as  
sumarized:

Team (has\_many (of all other data tables)  
Other Data (belongs\_to (team)

It’s not written this way, just easier to summarize.

I want to have two dropdowns populating the team names from the teams  
table with ID being the key during form submission for the other table  
data being retrieved.

When the data is retrieved, I want to render the data through a partial.

Here’s what I have as a generic start point:

Routes file

map.virtual\_matchups ‘/virtual\_matchups’, :controller =\> ‘teams’,  
:action =\> ‘virtual\_matchups’

Teams Controller

def virtual\_matchups  
@teamnames = Team.find(:all, :order =\> “name ASC”)  
@teamsearch = … ? …

render :partial =\> ‘datareturned’

respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @teams }  
end  
end

virtual\_matchups.html.erb file

 \<%= collection\_select(:team, :id, @teamnames, :id, :name, options ={:prompt =\> "- Select Team One"}, :class =\>"teamselect") %\> \<%= collection\_select(:team, :id, @teamnames, :id, :name, options ={:prompt =\> "- Select Team Two"}, :class =\>"teamselect") %\> 

I don’t have the form for in, nor do I have the submit button. I need  
to understand and have the following questions answered:

1. How do I designate each collection\_select so that when I perform my  
submit, the search knows which data to pull for each team?

2. Is my route setup correctly? Or, should I be using something  
similar to:  
map.resources :teams, :collection =\> {:virtual\_matchups =\> :get}

3. My @teamsearch instance variable is currently not setup because I’m  
not sure what params I will be sending to the model, or how to send it  
to multiple models the correct way. Because I’m pulling data from  
approx. 37 different tables for two separate teams, I’d like to do it  
properly and eagerly load if possible. What should I do?

4. I have not setup my datareturned partial yet because again, I’m  
unsure of what I need to do first to pull the data so if there’s  
something special I should do here, or should keep in mind when creating  
it, please let me know?

Any help or advice would be appreciated on this. I’ve tried spending a  
few days researching this on my own but I can’t seem to find anything  
that uses a side-by-side comparison example, nor at the same time  
pulling data from multiple models. As I’m a bit stumped, I could use  
some assistance getting through this bit.

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 26, 2009, 6:16pm UTC](https://www.ruby-forum.com/t/multiple-collection-selects-of-same-data-types-in-view/172715/2 "2009-07-26T18:16:53Z")

</div>

Okay, updating my current submit form. This may be correct usage for  
the two different teams - it appears to match correctly in the url at  
least:

# Test Virtual Matchups

 \<% form\_tag teams\_path, :method =\> :get do %\> \<%= collection\_select(:team, :id, @teamnames, :id, :name, options ={ :prompt =\> "- Select Team One" }, { :name =\> 'teamone[id]' } ) %\> \<%= collection\_select(:team, :id, @teamnames, :id, :name, options ={ :prompt =\> "- Select Team Two" }, { :name =\> 'teamtwo[id]' } ) %\> \<%= submit\_tag "Compare" %\> \<% end %\> 

I changed the :name so that team one is teamone[id] and team two is  
teamtwo[id] but I haven’t done anything with the information. I have  
still yet to figure that part out. Still working on it but let me know  
if anyone has answers to the previous questions and to this update.

thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 26, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/multiple-collection-selects-of-same-data-types-in-view/172715/3 "2009-07-26T19:21:12Z")

</div>

Okay, here’s my updated code thus far:

[http://pastie.org/559478](http://pastie.org/559478)

I moved all of this to a virtuals controller so I wouldn’t get confused  
during code creation.

It will give you a small idea of what I’m trying to do.

The current stuck point is that no data is returned and no errors. It  
could be due to associations or how my variables are being used - I’m  
just not sure.

Virtuals controller “has no” association with anything. It’s just a  
controller trying to pull data using the team model which is associated  
with everything.

Here are the transitions:

[http://localhost:3000/virtual\_matchups](http://localhost:3000/virtual_matchups)

Shows a page with two dropdown boxes and a compare button. All 120  
teams are populated in each dropdown.

When clicking compare after selecting two teams:

URL shows:

[http://localhost:3000/virtuals?teamone=48&teamtwo=97&commit=Compare](http://localhost:3000/virtuals?teamone=48&teamtwo=97&commit=Compare)

which is the url of the virtuals controller path and you can see by the  
URL the designator for each team ID is separated into two distinctly  
different names (teamone and teamtwo).

Look at the pastie code and let me know what you believe I’m doing  
wrong.

Thanks.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [July 26, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/multiple-collection-selects-of-same-data-types-in-view/172715/4 "2009-07-26T19:30:51Z")

</div>

[http://pastie.org/559478](http://pastie.org/559478)

Updated…

I got most of it figured out now I think…

I added the associations in for virtual and team and removed the old  
array in virtual model, and further changed the way index.html was  
formatted and returning results.

With this new code, I’m at least able to return two IDs and two team  
names for the search sent.
