Forum: Ruby on Rails queries

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-04-04 00:11
(Received via mailing list)
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?
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (squeegy)
on 2007-04-04 00:54
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?
1a9637acaccba514bf6c2cecf80c3f16?d=identicon&s=25 LeonB (Guest)
on 2007-04-04 21:01
(Received via mailing list)
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 Wayne <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.