# Best way to handle three way join

**URL:** <https://www.ruby-forum.com/t/best-way-to-handle-three-way-join/130717>\
**Category:** Rails\
**Created:** [March 2, 2008, 11:37am UTC](https://www.ruby-forum.com/t/best-way-to-handle-three-way-join/130717 "2008-03-02T11:37:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 2, 2008, 11:37am UTC](https://www.ruby-forum.com/t/best-way-to-handle-three-way-join/130717/1 "2008-03-02T11:37:30Z")

</div>

Hi -

I have 3 tables, A, B and C where A habtm C through B and vice versa  
(C habtm A).

I also have a Users table which habtm B

In other words, B can be filtered by the current user, and requires a  
userId to be valid (only a logged in user can add a new C record).

This is a testing application, where you have questions, answers and  
users. In this application, the answers are all cardinal values, so I  
want to be able to go backwards from answers to questions, thus the  
two-way habtm between A and C (A is Questions, C is answer choices,  
and every question has many answer choices, and every answer choice  
has many questions).

When user creates a new entry in C, for a given instance of A, a, I  
can do one of two things -

a) create a new C, insert it into a’s C collection, and in B’s  
beforeCreate, manually set the UserId

This requires the model to access a controller method to get the  
current user - is this bad Rails form?

b) use the user object to create a B instance, b, and set b’s C to c,  
and b’s A to a manually.

Which is better?

Or, is there a built in ActiveRecord way to manage 3 way joins?

Thanks,  
Dino
