Pete Yandell
2005-Jun-06 05:40 UTC
ActiveRecord find with :include more than one level deep
If I have three tables, A, B, and C and: A has_and_belongs_to_many B B belongs_to C can I do a find that will query out all the Bs and their corresponding Cs for a particular set of As (with a single SQL query)? Getting the A query to include the Bs is easy: A.find(:all, :conditions => [...], :include => [:b] but I can''t see a way to go a level deeper and include the Cs. Presuming I have to manually build some SQL for this, how do I then use that SQL to sensibly populate Rails objects such that A.bs will contain the array of Bs, and A.bs[...].c will contain the associated C for a particular B? (I hope all these ABCs don''t make the description so abstract that nobody can follow it!) Cheers, Pete Yandell