Q: find :include


#1

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 :slight_smile:


#2

On 27 Feb 2009, at 11:26, removed_email_address@domain.invalid 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


#3

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


#4

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-rails-2-1-the-first-full-tutorial-part-2

So 2 queries is ok! :slight_smile:

thanks guys