How to SELECT from multiple talbes with AR#find and associat

So I want to SELECT from multiple tables, so as to be able to do
“SELECT FROM foo, bar WHERE foo.id=bar.id AND bar.baz=23”.

How do I do it with AR#find?

Also, I need this for associations. has_many et. al. have :finder_sql,
which is nice, but feels like a Pyrrhic: it’s basically doing the
association in pure SQL, defeating the purpose of AR.

Look at include option in find method.

Model.find(:all, :include => [“other_model”])

On 7/22/06, Bruno C. [email protected] wrote:

Look at include option in find method.

From the docs:

“:include: Names associations that should be loaded alongside using
LEFT OUTER JOINs. The symbols named refer to already defined
associations. See eager loading under Associations.”

So it wasn’t intended for this but… it works! :slight_smile:

My guess is that the :include adds table bar to the SELECT, so WHERE
can refer to it in the same query when you specify it in conditions.

Using it qualifies as a hack, imho :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs