Hi –
On Fri, 15 Dec 2006, Patrick A. wrote:
machines.model
AS t1_r3, machines.serial
AS t1_r4, machines.type
AS t1_r5, machines.year
AS t1_r6, machines.hours
AS t1_r7,
machines.miles
AS t1_r8, machines.capacity
AS t1_r9,
machines.price
AS t1_r10, machines.description_fr
AS t1_r11,
machines.description_en
AS t1_r12, machines.description_es
AS
t1_r13, machines.description_ar
AS t1_r14, machines.added_date
AS
t1_r15, machines.update_date
AS t1_r16 FROM brands LEFT OUTER JOIN
machines ON machines.brand_id = brands.id WHERE (category_id=50)
Doesn’t AR squish that down into one brand with multiple
eagerly-loaded machines? I’ve tried to do a mini-replica of your
domain model (using household appliances, which may or may not be your
actual domain
On the SQL side:
mysql> SELECT brands.id
AS t0_r0, brands.name
AS t0_r1,
machines.id
AS t1_r0, machines.name
AS t1_r1, machines.brand_id
AS t1_r2, machines.category_id
AS t1_r3 FROM brands LEFT OUTER JOIN
machines ON machines.brand_id = brands.id WHERE (category_id = 1);
±------±------±------±------------±------±------+
| t0_r0 | t0_r1 | t1_r0 | t1_r1 | t1_r2 | t1_r3 |
±------±------±------±------------±------±------+
| 2 | GE | 3 | GE Washer 1 | 2 | 1 |
| 2 | GE | 4 | GE Washer 2 | 2 | 1 |
±------±------±------±------------±------±------+
2 rows in set (0.00 sec)
but on the AR side (this is what generated that SQL):
Brand.find(:all, :include => “machines”,
:conditions => “category_id = 1”)
=> [#<Brand:0xb75f8aa4 @attributes={“name”=>“GE”, “id”=>“2”},
@machines=[#<Machine:0xb75f87ac @attributes={“name”=>“GE Washer 1”,
“brand_id”=>“2”, “id”=>“3”, “category_id”=>“1”}>,
#<Machine:0xb75f84f0
@attributes={“name”=>“GE Washer 2”, “brand_id”=>“2”, “id”=>“4”,
“category_id”=>“1”}>]>]
there’s just one Brand in the results. So I guess something is
different in my replica, but I’m not sure what.
(Ultimately, by the way, I imagine you could wrap this in a has_many
:through construct; but of course one wants to know why it’s not
working.)
David
–
Q. What’s a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)