Alder Green
2006-Jul-22 17:06 UTC
[Rails] How to SELECT from multiple talbes with AR#find and associations?
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. -- -Alder
Bruno Celeste
2006-Jul-22 17:33 UTC
[Rails] How to SELECT from multiple talbes with AR#find and associations?
Look at include option in find method. Model.find(:all, :include => ["other_model"]) On 7/22/06, Alder Green <alder.green@gmail.com> wrote:> 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. > > -- > -Alder > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Alder Green
2006-Jul-22 18:11 UTC
[Rails] How to SELECT from multiple talbes with AR#find and associations?
On 7/22/06, Bruno Celeste <bruno.celeste@gmail.com> 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! :) 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 :-)> Model.find(:all, :include => ["other_model"]) > > On 7/22/06, Alder Green <alder.green@gmail.com> wrote: > > 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. > > > > -- > > -Alder > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -Alder
Reasonably Related Threads
- [AR] #{id} namespace visibility used in finder_sql
- create separate plots by factors
- Xen over SuSE 10.3 - Can''t find drive to boot with...
- Maintaining and securing the "Perfect Rails/Debian/Lighttpd Stack"
- Creating a "Foo has_many bars" association where bar isn''t a model.