Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > rails: many to many agony

Reply
Thread Tools

rails: many to many agony

 
 
Graham Arrowsmith
Guest
Posts: n/a
 
      02-17-2005
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
 
Reply With Quote
 
 
 
 
Dave Burt
Guest
Posts: n/a
 
      02-18-2005
"Graham Arrowsmith" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Luca Pireddu
Guest
Posts: n/a
 
      02-18-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Agony!!! Agony !!!! dwacon Computer Support 6 04-10-2005 08:52 PM
Wireless agony Martin Wireless Networking 0 09-10-2004 06:20 PM
Cisco PIX many-to-many NAT problem Fredy Kuenzler Cisco 4 06-15-2004 07:06 PM
Digital prints-the Agony of choice!! mark.worthington Digital Photography 4 04-22-2004 11:03 PM
Nikon 5700 flash agony Phil Rose Digital Photography 7 10-20-2003 11:27 AM



Advertisments