Joins or relationships?

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

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

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?

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

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

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” :slight_smile: would love to know if there’s another way to do
this easily.

Cheers,
Mohit.

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

Steve,

Yes, helps big time! Thank you.

Stuart