Ruby On Rails - Foreign Key Lookup

Posted on 2007-10-14
Last Modified: 2012-08-13
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.



Question by:cmgtech
    LVL 60

    Accepted Solution

    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

    class Purchase < ActiveRecord::Base
          belongs_to :wine

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

    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="<%= %>"
                        <%= ' selected' if == @purchase.wine_id %>>
                        <%= %> -
                        <%= wine.sous_appel.to_str.length > 0 ? wine.sous_appel : wine.appelation %> -
                        <%= wine.year %>
            <% end %>
      <%= 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, sous-appelation and year form the label,
    the 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




    Author Comment

    perfect.  you should start a blog of concise solutions to problems.

    Author Comment

    oh, and thanks so much.
    LVL 60

    Expert Comment

    by:Geert Bormans

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
    Article by: narshlob
    If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now