Ok, I have a many_to_many relationship between two tables (A, B) implemented by a join table (J). J has two columns: a_id and b_id.
Currently, I use a query like this: <code> recs = A.find :all, :select => 'A.*', :joins => ', B, J', :conditions => 'A.id = J.a_id AND J.b_id = B.id AND B.somefield = somevalue ' </code>
That works well, but is there a more Rails like way to do it?
Also, is there a more efficient way to do it? Notice in my RoR code, I'm joining 3 tables together in a single SQL statement. Would it be faster to select ids in an SQL call joining only 2 tables, then calling A.find :all, ids?
In code: <code> ids_array = "SELECT J.a_id FROM J, B WHERE J.b_id = B.id AND B.somefield = somevalue" recs = A.find :all, ids_array </code>
How do I efficiently execute that query and get an array of ids in return?
Thanks for the help.