Queries


#1

Hi,

I have this code:

for user in User.find(:all, :include => :declarations )

  for declaration in user.declarations
    pay += declaration.share($my)

    if declaration.my?
      paid = declaration.amount
    end
  end
end

end

But that generates all this queries: http://pastie.caboo.se/51507

Is there a way to generate a lot less queries?


#2

LeonB wrote:

Hi,

I have this code:

for user in User.find(:all, :include => :declarations )

  for declaration in user.declarations
    pay += declaration.share($my)

    if declaration.my?
      paid = declaration.amount
    end
  end
end

end

But that generates all this queries: http://pastie.caboo.se/51507

Is there a way to generate a lot less queries?

You log there doesn’t have the SQL for the loading of the users. You
have the proper :include options but it doesn’t appear to be working.
What SQL is generated from the Users query?


#3

Thanks for the reply. That’s this huge query:

SELECT users.“id” AS t0_r0, users.“email” AS t0_r1, users.“firstname”
AS t0_r2, users.“lastname” AS t0_r3, users.“password” AS t0_r4,
users.“account_number” AS t0_r5, users.“created_at” AS t0_r6,
users.“updated_at” AS t0_r7, declarations.“id” AS t1_r0,
declarations.“user_id” AS t1_r1, declarations.“amount” AS t1_r2,
declarations.“description” AS t1_r3, declarations.“created_at” AS
t1_r4, declarations.“updated_at” AS t1_r5 FROM users LEFT OUTER JOIN
declarations_payers ON (declarations_payers.user_id = users.id) LEFT
OUTER JOIN declarations ON (declarations.id =
declarations_payers.declaration_id)

On Apr 4, 12:54 am, Alex W. removed_email_address@domain.invalid