def A belongs_to B belongs_to C end I want to select all the A where a.b.name LIKE "x" and a.c.name LIKE Y. Is it possible to do this with one SQL statement? -- Posted via http://www.ruby-forum.com/.
Gleb Mazovetskiy wrote:> def A > belongs_to B > belongs_to C > end > > I want to select all the A where a.b.name LIKE "x" and a.c.name LIKE Y. > Is it possible to do this with one SQL statement?Sure: SELECT * from a LEFT JOIN b ON b.id = a.b_id LEFT JOIN c ON c.id = a.c_id WHERE b.name LIKE ''X'' -- did you mean ''%X%''? AND c.name LIKE ''Y'' Writing a suitable ActiveRecord find statement is left as an exercise to the student. :) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
2009/9/11 Gleb Mazovetskiy <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > def A > belongs_to B > belongs_to C > end > > I want to select all the A where a.b.name LIKE "x" and a.c.name LIKE Y. > Is it possible to do this with one SQL statement? > --Why do you want to use sql? Let rails do it for you, something like find(:all, :include => [:b, :c], :conditions => [b.name like ? and c.name like ?, ''x'', ''y''] ) Colin