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

Posted on 2007-10-19
Medium Priority
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
  • 2
  • 2

Author Comment

ID: 20113879
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.

http://railscasts.com/episodes/73 -part 1
http://railscasts.com/episodes/74 -part 2
http://railscasts.com/episodes/75 -part 3


LVL 60

Accepted Solution

Geert Bormans earned 2000 total points
ID: 20147538
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 = Artist.new
    @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.id %>">
                    <%= 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

ID: 20149044
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
ID: 20149095

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

807 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