What's the difference between “includes” and “preload” in an ActiveRecord query?

后端 未结 3 652
傲寒
傲寒 2020-12-13 03:49

I\'m struggling to find a comparison of includes() and preload() for ActiveRecord objects. Can anyone explain the difference ?

3条回答
  •  悲&欢浪女
    2020-12-13 04:41

    For detailed understanding refer this blog.

    In short:

    Preload loads the association data in a separate query.

    User.preload(:posts).to_a
    
    # => SELECT "users".* FROM "users" SELECT "posts".* FROM "posts"  WHERE "posts"."user_id" IN (1)
    

    Includes loads the association data in a separate query just like preload. However it is smarter than preload. But in some cases it combines the queries.

    How includes is smarter?

    we can’t use post table in where condition. Following query will result in an error.

    User.preload(:posts).where("posts.desc='ruby is awesome'")
    
    # =>
    SQLite3::SQLException: no such column: posts.desc:
    SELECT "users".* FROM "users"  WHERE (posts.desc='ruby is awesome')
    

    But we can use post table in where query with include

    User.includes(:posts).where('posts.desc = "ruby is awesome"').to_a
    
    # =>
    SELECT "users"."id" AS t0_r0, "users"."name" AS t0_r1, "posts"."id" AS t1_r0,
           "posts"."title" AS t1_r1,
           "posts"."user_id" AS t1_r2, "posts"."desc" AS t1_r3
    FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"
    WHERE (posts.desc = "ruby is awesome")
    

    As you can see includes switches from using two separate queries to creating a single LEFT OUTER JOIN to get the data. And it also applied the supplied condition.

提交回复
热议问题