Using :join option in find

I’ve been having problems getting one association to work in my models
setup. I believe becasue of it not having the standard name
convention. Although I’m not sure why there seems to be no way to
designate the naming used in the model associations.

Anyway, after searching for a bit I found that you can add :join to a
find statement and by doing so I was able to enjoy all of the AR magic
in the other models and the one that is a rogue :). One caveat in
the material though to adding the :join , it said next to this option
“rarely used”. So I’m wondering what “rarely used” would mean ? Is it
because it’s a bad thing or because there are better ways to do it.
Is there a way to add a join statement in the model association code
?

Stuart

Stuart Fellowes wrote:

I’ve been having problems getting one association to work in my models
setup. I believe becasue of it not having the standard name
convention. Although I’m not sure why there seems to be no way to
designate the naming used in the model associations.

Anyway, after searching for a bit I found that you can add :join to a
find statement and by doing so I was able to enjoy all of the AR magic
in the other models and the one that is a rogue :). One caveat in
the material though to adding the :join , it said next to this option
“rarely used”. So I’m wondering what “rarely used” would mean ? Is it
because it’s a bad thing or because there are better ways to do it.
Is there a way to add a join statement in the model association code
?

If you need a join then there’s nothing wrong with using it, however the
implication is that it’s a lot easier to write bob.articles than to
write an sql statement that fetches bob’s articles.
There is also eager loading, so Person.find_by_name ‘bob’, :include =>
:articles will load bob as well as all of his articles in one go.
If you associations don’t have the standard namind there are options to
override the defaults, eg

belongs_to :thing, :class_name => ‘Widget’, :foreign_key => ‘thing_key’

If you have an association where the thing_key column of the database is
pointing at an object of class Widget (and you be able to access it as
bob.thing

Fred