Re: junction table question

Walter Lee Davis
Tue, 09 Aug 2011 18:42:55 -0000

On Aug 9, 2011, at 2:25 PM, Mickey Cowden wrote:

> I''m looking into how to implement a relationship between some
models,
> and I think I need to use a junction table.  From the documentation
> I''ve
> read, a junction table doesn''t require a model.

That''s in the basic has_and_belongs_to_many relationship. You just
provide a table with modela_id and modelb_id in it, and the
relationship takes care of the rest.

> But from my controller,
> I''m not sure how to get the data I need.  Here''s a
description of my
> models and what I need in my controller:
>
> Models
> 1. Event
> 2. Site
> 3. Participant

This is a rich relationship, described in the "has_many :through"
relationship. This is a full model-backed join object, not the
lightweight join table in habtm. You will need a model (although you
won''t need a separate controller) to get at any of the data stored in
this relationship.

Walter

>
> Relationships:
> 1. An event can have multiple sites (example video conferencing)
> 2. A site can have multiple events
> 3. Participant goes to an event at a location
>
> Database Tables:
> 1. event_sites (:id, :event_id, :site_id)
> 2. participants (:id, :event_site_id, ...)
>
> And in my participant controller, I need to get the sites available
> for
> a particular event the participant is signing up for (new action).
> Any
> thoughts on how I can get that data from the junction table, or a
> better
> approach in general?  Thanks.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-
> talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> .
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
> .
>