Forum: Ruby on Rails Q: find :include

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.
B2099e6049b78ac28d07e5d8f0a01300?d=identicon&s=25 rails.nerd@gmail.com (Guest)
on 2009-02-27 12:27
(Received via mailing list)
So I have....

  class Post < ActiveRecord::Base
    belongs_to :user
  end

  @posts = Post.find :all, :include=>:user

.... but when I look at my logs I see this:

  Post Load (2.4ms)   SELECT * FROM `posts`
  User Load (0.2ms)   SELECT * FROM `users` WHERE (`users`.`id` = 1)

I was expecting one combined SQL statement?

Any ideas?
thanks :)
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-27 12:31
(Received via mailing list)
On 27 Feb 2009, at 11:26, rails.nerd@gmail.com wrote:

>
>  Post Load (2.4ms)   SELECT * FROM `posts`
>  User Load (0.2ms)   SELECT * FROM `users` WHERE (`users`.`id` = 1)
>
> I was expecting one combined SQL statement?
>
Not since rails 2.1

Fred
6b46f4eed76df33a3ffacf87bc9f1079?d=identicon&s=25 vimal (Guest)
on 2009-02-27 12:32
(Received via mailing list)
what exactly do you want to do???

for me it looks like you want all the posts for the particular user
then it should be @posts = User.posts
B2099e6049b78ac28d07e5d8f0a01300?d=identicon&s=25 rails.nerd@gmail.com (Guest)
on 2009-02-27 13:55
(Received via mailing list)
> what exactly do you want to do???

I just didn't want to make a new query everytime i accessed a user of
a post

However, Fred is right
http://www.akitaonrails.com/2008/5/26/rolling-with...

So 2 queries is ok! :)

thanks guys
This topic is locked and can not be replied to.