Managing joins


#1

Hi there,

Another rails newbie here, trying to get started on some many to
many :through structures (solid background in dB, php and asp). On
the database and model side of things, everything is relatively
clear. However, when it comes to the interactions on the web
interface, I’m still not getting something.

I’ve seen a lot of good examples for adding items that are linked
to an existing item in another table, and others for creating
multiple related items in one shot. However, what I haven’t yet
found is the means to link existing objects together via a join
table. It will definitely be a join table rather an a
has_many_and_belongs_to since there will be some additional data held
in the join table.

The simplified use case looks something like this :

http://homepage.mac.com/eableson/xfer/simple_use_case.jpg

So I have people creating projects and consultants in their own
independent tables (and models and controllers). Project planners
need to assign consultants to projects and then assign timeblocks
(managed in 1/2 day increments) which will be linked to both the
consultant and the project.

Focussing on the consultant to project links I have the following (I
still haven’t worked out the tertiary join for the timeblocks to both
missions and consultants):
class Consultant < ActiveRecord::Base
has_many :timeblocks
has_many :missions
end

class Assignment < ActiveRecord::Base
belongs_to :consultant
belongs_to :mission
end

class Mission < ActiveRecord::Base
has_many :consultants
has_many :timeblocks, :through => :consultants
end

The project planner will pull up a project page, and assign a
consultant (potentially adding several) to the project. Under the
project information, each assigned consultant will be displayed, and
a calendar with selecting timeblocks as checkboxes so that I can pick
the dates he’ll be working on the project. Something like:

http://homepage.mac.com/eableson/xfer/webpage_models.jpg

I was thinking of having a select list with an add button to build
the association, but I haven’t figured out how to build the necessary
rhtml code to handle this. If I were working at a lower layer in
php, I know exactly how I would accomplish this, but I’m still have a
few difficulties in finding the appropriate Rails abstraction to
express this.

Hints, tips?

Erik A.