Ruby on Rails: Dealing with many-to-many relationships in one view

Posted on 2007-10-19
Last Modified: 2008-01-09

I am creating an application to learn Rails and I've come to a point that I really can;t find any help on.

I have a many-to-many relationship between SHOW and ARTIST associated by APPEARANCE.
I have it all set up to Create SHOWs, ARTISTs, and then join them with APPEARANCEs (scaffolding-style), but as a workflow it doesn't seem like it would fly in actual practice.

While creating a SHOW, I'd like to also be able to add as many APPEARANCES as I want (we'll say 3 here to simplify it).  So I imagine the view looking like:
1) All the NEW SHOW information
2) 3 dropdown controls populated by ARTISTS
---When this screen is finally saved, the Create method should create the appropriate APPEARANCE records.  

On my own after this works, I'm hoping that I'll be able to apply the same technique to create new ARTIST records from the NEW SHOW view.  But again, if I get some help just with the part above, I should be able to figure it out from there... although I am feeling really incompetent right now.

Any help or insight will be greatly appreciated.


Question by:cmgtech

    Author Comment

    I have found a good video tutorial website that does good one-to-many tutorial, I'll work on that to see if I can extned it to work with many-to-many... though any help is still apreciated. -part 1 -part 2 -part 3


    LVL 60

    Accepted Solution

    Hi Mark

    there is an article here
    but I don't like it, since it has its proprietary link table, and I assume you want a default link table

    I gave it some thoughts, and here is how I would get started

    show model
    class Show < ActiveRecord::Base

    artist model
    class Artist < ActiveRecord::Base

    controller for artist
      def new
        @artist =
        @shows = Show.find_all

    <h1>New artist</h1>

    <% form_tag :action => 'create' do %>
    <p><select name="artist[appears_in]">
            <% @shows.each do |show| %>
                  <option value="<%= %>">
                        <%= show.title %>
            <% end %>

      <%= render :partial => 'form' %>
      <%= submit_tag "Create" %>
    <% end %>

    all you need to do then is make sure that the appears_in= method exists in the model
    so you need to extend the artist model

    class Artist < ActiveRecord::Base
          def appears_in= showid
                shows << Show.find(showid)

    this will definitely work,
    but I am afraid that the link will be stored in the link table
    prior to the actual storage of the new artist
    (which in the end could lead to erroneous links)

    I have to think a bit about this,
    but it gives you something to get started

    maybe you need to work with stub methods in the artist model
    that only get written on-save

    I definitely think that writeable methods in the model are the safest solution
    ... certainly a lot less annoying than the method in the referenced article

    weird, I never really came accross this, and there is hardly any literature about it :-(




    Author Comment

    Yeah, I was surprised at how hard it was to find documentation on it.

    Your solution worked out for me though, thanks for answering such a stale question.


    LVL 60

    Expert Comment

    by:Geert Bormans

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    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…
    In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below:…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now