# Rails: many to many agony

**URL:** <https://rubytalk.org/t/rails-many-to-many-agony/16574>\
**Category:** ruby-talk\
**Created:** [17 February 2005 23:49 UTC](https://rubytalk.org/t/rails-many-to-many-agony/16574 "2005-02-17T23:49:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![G\_Arrowsmith](https://avatars.discourse-cdn.com/v4/letter/g/22d042/32.png) [@G\_Arrowsmith](https://rubytalk.org/u/G_Arrowsmith)\
**Post date:** [17 February 2005 23:49 UTC](https://rubytalk.org/t/rails-many-to-many-agony/16574/1 "2005-02-17T23:49:47Z")

</div>

I have two classes: Projects and Company. There are many to many  
relationships between companies and project and are joined by table  
company\_projects

Thing is when I render out a select list I want to make the related  
dropdown items selected :

\<select name="project[company\_id]"\>  
&nbsp;&nbsp;&nbsp;\<% @companies.each do |company| %\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<option value="\<%= [company.id](http://company.id) %\>" \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= ' selected' if [company.id](http://company.id) == @project.companies.id %\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%=company.companyName %\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</option\>  
&nbsp;&nbsp;&nbsp;\<% end %\>  
\</select\>

However I can't seem to be able to get the [project.companies.id](http://project.companies.id) (ie  
company\_project.company\_id) for the related project.

I know this is pretty darn simple but I can't seem to crack it.

Any help would be appreciated!

Graham

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [18 February 2005 12:34 UTC](https://rubytalk.org/t/rails-many-to-many-agony/16574/2 "2005-02-18T12:34:47Z")

</div>

> I have two classes: Projects and Company  
> [with a many-to-many relationship]
> 
> \<select name="project[company\_id]"\>  
> &nbsp;&nbsp;\<% @companies.each do |company| %\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<option value="\<%= company.id %\>" \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= ' selected' if company.id == @project.companies.id %\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= ' selected' if @project.companies.include? company %\>

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%=company.companyName %\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</option\>  
> &nbsp;&nbsp;\<% end %\>  
> \</select\>

(Does that make a multiple-select? I thought I had to add the html\_options  
parameter {:multiple =\> 'multiple'}.)

Cheers,  
Dave

> **···**
>
> "Graham Arrowsmith" \<g.arrowsmith@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Luca\_Pireddu](https://avatars.discourse-cdn.com/v4/letter/l/8491ac/32.png) [@Luca\_Pireddu](https://rubytalk.org/u/Luca_Pireddu)\
**Post date:** [18 February 2005 16:44 UTC](https://rubytalk.org/t/rails-many-to-many-agony/16574/3 "2005-02-18T16:44:47Z")

</div>

Graham Arrowsmith wrote:

> I have two classes: Projects and Company. There are many to many  
> relationships between companies and project and are joined by table  
> company\_projects
> 
> Thing is when I render out a select list I want to make the related  
> dropdown items selected :
> 
> \<select name="project[company\_id]"\>  
> &nbsp;&nbsp;&nbsp;\<% @companies.each do |company| %\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<option value="\<%= company.id %\>" \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= ' selected' if company.id == @project.companies.id %\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%=company.companyName %\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</option\>  
> &nbsp;&nbsp;&nbsp;\<% end %\>  
> \</select\>
> 
> However I can't seem to be able to get the project.companies.id (ie  
> company\_project.company\_id) for the related project.
> 
> I know this is pretty darn simple but I can't seem to crack it.
> 
> Any help would be appreciated!
> 
> Graham

I've just begun using activerecord, so take my suggestion with a grain of  
salt 🙂

project.companies should return an array of companies, no? That makes it  
wrong to say project.companies.id. I think you want something like

\<%= ' selected' if @project.companies.member?(company) %\>

Array#member? uses == to test for equality, and I think I remember reading  
that AR overrides == to test for the equality of the "id" field so the  
comparison should work.

Hope that helps.

Luca
