rails: many to many agony

Discussion in 'Ruby' started by Graham Arrowsmith, Feb 17, 2005.

  1. 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]">
    <% @companies.each do |company| %>
    <option value="<%= company.id %>" >
    <%= ' selected' if company.id == @project.companies.id %>
    <%=company.companyName %>
    </option>
    <% 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
    Graham Arrowsmith, Feb 17, 2005
    #1
    1. Advertising

  2. Graham Arrowsmith

    Dave Burt Guest

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


    <%= ' selected' if @project.companies.include? company %>

    > <%=company.companyName %>
    > </option>
    > <% end %>
    > </select>


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

    Cheers,
    Dave
    Dave Burt, Feb 18, 2005
    #2
    1. Advertising

  3. Graham Arrowsmith

    Luca Pireddu Guest

    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]">
    > <% @companies.each do |company| %>
    > <option value="<%= company.id %>" >
    > <%= ' selected' if company.id == @project.companies.id %>
    > <%=company.companyName %>
    > </option>
    > <% 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
    Luca Pireddu, Feb 18, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. D.
    Replies:
    9
    Views:
    168
    Jamis Buck
    Nov 4, 2004
  2. Replies:
    6
    Views:
    181
    Michael Campbell
    Mar 17, 2005
  3. Greg McIntyre

    Rails + Amrita, Rails + XTemplate?

    Greg McIntyre, Apr 28, 2005, in forum: Ruby
    Replies:
    2
    Views:
    125
    Greg McIntyre
    Apr 28, 2005
  4. Michael Schuerig
    Replies:
    3
    Views:
    220
    Carl Youngblood
    Jun 17, 2005
  5. Brian Takita
    Replies:
    1
    Views:
    278
    Zed A. Shaw
    Sep 5, 2005
Loading...

Share This Page