# Joins or relationships?

**URL:** <https://www.ruby-forum.com/t/joins-or-relationships/95800>\
**Category:** Rails\
**Created:** [June 14, 2006, 12:54am UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800 "2006-06-14T00:54:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 12:54am UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/1 "2006-06-14T00:54:28Z")

</div>

I have a table called Post (not the actual name) where users input  
various  
piece of information, Most of the input is done by dynamic select’s  
(drop  
downs).  
As an example:  
User would choose  
Field: Input: Values that go  
into  
Post table  
Category Full Time 2  
Length 45 Days 4  
State Colorado 7  
Skill Rails 1

The values are derived from other tables, Static tables that would only  
be  
accessed via form fields.

Since I don’t think there should or could be any real relationships  
between  
these “static” tables (only administrator can CRUD these tables) and the  
Post table, would the correct option  
for displaying results (list) and editing be doine via joins ?

TIA  
Stuart

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/2 "2006-06-14T12:13:25Z")

</div>

Is this a poorly constructed question ? Still trying to figure this  
one  
out.  
In PHP I would have used left joins, but it seems (from what I’ve read  
they  
are not popular in RoR.

Stuart

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 12:56pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/3 "2006-06-14T12:56:19Z")

</div>

I don’t understand this question either, can you provide a bit more  
information on what you’re trying to achieve?

A bit off-topic:

> In PHP I would have used left joins, but it seems (from what I’ve read  
> they  
> are not popular in RoR.

Left Joins are used in Rails to help implement eager loading of  
associations (if I’m not mistaken). Where have you read they’re not  
popular in RoR?

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/4 "2006-06-14T14:42:30Z")

</div>

Hopefully this will explain it better.  
I’ll break it down to a smaller example.

I have 2 tables, “users” and “states”

In the users table I have a field “state” (where the user would input  
their  
home state)  
However, the value for the state field in the user table is a number  
value,  
drawn from the id field of the corresponding state in the states table.  
The  
states table is static in the sense that it is never going to be written  
to  
or edited by users.

users table

| user | state |
| --- | --- |

1 3

states table

| id | state |
| --- | --- |

3 California

So for display purposes, search forms, etc the state id (in this case 3  
for  
California) would not be shown, only the label (California).  
So in some ways I can see doing a “has one, belongs to” but in the past  
I’ve  
used joins.  
In Agile Web D. , when the author mentions joins he also says  
something like, but you probably won’t be using these very much and  
references table relationships (HABTM, etc).

Stuart

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/5 "2006-06-14T15:08:33Z")

</div>

Mohit, Thank you for the reply. Actually I have not done the “four  
days…”  
tutorial. I think today I will :).  
Yesterday I did the “Rolling with Rails”, cookbook2 which btw I thought  
was  
very good. Rolling also uses a categories table which (I’m guessing) is  
similar to the one in Four days. In Rolling , categories can be edited,  
and  
added to. It has a field that associates recipes with a category , and  
the  
recipes table has a field for categories. I’m just not sure if it  
applies  
to my situation and while I will try it that way the real motivation for  
my  
question is “what’s the most practical way” and “what way makes the most  
sense in terms of say overhead and simplicity”.

Stuart

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/6 "2006-06-14T14:52:27Z")

</div>

Dark A. wrote:

> never going to be written to or edited by users.  
> -----|---------------|  
> Stuart  
> Hi Stuart,

Since I know that you used the “four days with rails” tutorial, can’t  
you use the method that is used for displaying the categories for the  
To-Do list. It sounds that your scenario is quite similar, is it not?  
The only difference as I see it is that the “states table” is static, as  
against the “categories table” in the four days example (which should  
make your life easier, I’d think).

Of course, I’m still new to the concepts, so I hope I’m not annoying the  
“council elders” 🙂 would love to know if there’s another way to do  
this easily.

Cheers,  
Mohit.

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/7 "2006-06-14T15:11:44Z")

</div>

On 6/14/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/8 "2006-06-14T15:27:16Z")

</div>

You can use joins if you want to - but for your example, you wouldn’t  
need them. If you have:

User - belongs\_to :state

Then you can just load the state directly:  
user = User.find(1)  
user.state.state -\> “California”

(Incidently, your ‘state’ column in the ‘users’ table should be  
‘state\_id’)

This will however, make 2 db calls; one to get the user, the next to  
load the state when you reference it.

If you’re concerned about the performance in this situation, you can use  
eager loading:

user = User.find(1, :include =\> [:state])  
user.state.state -\> “California”

In this example, there is only one query; Rails will do a join to get  
the state on the first find query.

If you _really_ want to have your own joins, you can:  
user = User.find(1, :joins =\> “LEFT JOIN states ON users.state\_id =  
states.id”)

However, the reason this is rarely used is because, firstly you don’t  
need it most of the time and it also means that the resulting records do  
not directly map to models i.e. the columns returned will be a  
combination of User and State.

Hope that helps,  
Steve

---

<div class="post-metadata">

**Author:** ![Dark\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@Dark\_A](https://www.ruby-forum.com/u/Dark_A)\
**Post date:** [June 14, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/joins-or-relationships/95800/9 "2006-06-14T15:33:31Z")

</div>

Steve,

Yes, helps big time! Thank you.

Stuart
