Forum: Ruby on Rails Joins or relationships ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dark Ambient (Guest)
on 2006-06-14 00:54
(Received via mailing list)
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
Dark Ambient (Guest)
on 2006-06-14 12:13
(Received via mailing list)
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
Ryan Allen (Guest)
on 2006-06-14 12:56
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?
Dark Ambient (Guest)
on 2006-06-14 14:42
(Received via mailing list)
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 Development , 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 Sindhwani (Guest)
on 2006-06-14 14:52
(Received via mailing list)
Dark Ambient 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.
Dark Ambient (Guest)
on 2006-06-14 15:08
(Received via mailing list)
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 Ambient (Guest)
on 2006-06-14 15:11
(Received via mailing list)
On 6/14/06, Dark Ambient <sambient@gmail.com> wrote:
3aafbe7cb5c02d2a214166630afe90ac?d=identicon&s=25 Stephen B. (steveb)
on 2006-06-14 15:27
(Received via mailing list)
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
Dark Ambient (Guest)
on 2006-06-14 15:33
(Received via mailing list)
Steve,

Yes, helps big time! Thank you.

Stuart
This topic is locked and can not be replied to.