Multiple collection selects of same data types in view?

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

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 }

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.


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

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.


Okay, here’s my updated code thus far:

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:


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:


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



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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs