Joins or relationships?

I have a table called Post (not the actual name) where users input
piece of information, Most of the input is done by dynamic select’s
As an example:
User would choose
Field: Input: Values that go
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
accessed via form fields.

Since I don’t think there should or could be any real relationships
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 ?


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


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
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?

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
home state)
However, the value for the state field in the user table is a number
drawn from the id field of the corresponding state in the states table.
states table is static in the sense that it is never going to be written
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
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
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).


Mohit, Thank you for the reply. Actually I have not done the “four
tutorial. I think today I will :).
Yesterday I did the “Rolling with Rails”, cookbook2 which btw I thought
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,
added to. It has a field that associates recipes with a category , and
recipes table has a field for categories. I’m just not sure if it
to my situation and while I will try it that way the real motivation for
question is “what’s the most practical way” and “what way makes the most
sense in terms of say overhead and simplicity”.


Dark A. wrote:

never going to be written to or edited by users.
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” :slight_smile: would love to know if there’s another way to do
this easily.


On 6/14/06, Dark A. [email protected] wrote:

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

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 =”)

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,


Yes, helps big time! Thank you.