Re: Using :join option in find

Frederick Cheung
Mon, 25 Sep 2006 13:17:49 -0000

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

--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---