• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4548
  • Last Modified:

Ruby On Rails - Foreign Key Lookup

I have a one-to-many relationship between a 'file' and a 'directory'.  What I'd like is that in the 'file' scaffolding (maybe view is more appropriate?), a dropdown with all 'directory' listings.  I'm having a many-to-many issue, but I'm hoping that once I figure out one-to-many, I should be able to figure out many-to-many.

The best I can find on the internet show that I put a belongs_to in file model and a has_many in directory model.  How do I take it to the final step and create a dropdown that will allow me to assign directories to files?  

I assume that I modify the 'new' method in the file controller, but I'm having a hard time with syntax.  After modifying the controller, do I have to do anything to the view?

Any help is appreciated, or a website that outlines how to do this.  I have looked around on the web and can;t find anything that spells this out for me.  I'm not much of a programmer so I might not be assuming enough.

Thanks,

Mark

0
cmgtech
Asked:
cmgtech
  • 2
  • 2
1 Solution
 
Geert BormansCommented:
Here is an example
(it is from my course, purchases and wine, but I assume you can easiy adapt it to your needs)

class Wine < ActiveRecord::Base
      has_many :purchases
end

class Purchase < ActiveRecord::Base
      belongs_to :wine
end

in the purchase_controller,  I have
...
  def edit
    @wines = Wine.find_all
    @purchase = Purchase.find(params[:id])
  end
...
  def new
    @purchase = Purchase.new
    @wines = Wine.find_all
  end
...

and in the purchase view for edit, I have
<h1>Editing purchase</h1>

<% form_tag :action => 'update', :id => @purchase do %>
  <p><select name="purchase[wine_id]">
        <% @wines.each do |wine| %>
              <option value="<%= wine.id %>"
                    <%= ' selected' if wine.id == @purchase.wine_id %>>
                    <%= wine.name %> -
                    <%= wine.sous_appel.to_str.length > 0 ? wine.sous_appel : wine.appelation %> -
                    <%= wine.year %>
              </option>
        <% end %>
  </select></p>
  <%= render :partial => 'form' %>
  <%= submit_tag 'Edit' %>
<% end %>

<%= link_to 'Show', :action => 'show', :id => @purchase %> |
<%= link_to 'Back', :action => 'list' %>

Here is where I create the dropdownlist
the wine.name, sous-appelation and year form the label,
the wine.id is hidden in the option value attribute

Since this is the edit operation, there already is a wine selected,
hence the "selected" attribute in the dropdownlist

I hope this helps you on your way

cheers

Geert

0
 
cmgtechAuthor Commented:
perfect.  you should start a blog of concise solutions to problems.
0
 
cmgtechAuthor Commented:
oh, and thanks so much.
0
 
Geert BormansCommented:
welcome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now