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.

So includes changes from two queries to a single query in some cases. By default for a simple case it will use two queries. Let’s say that for some reason you want to force a simple includes case to use a single query instead of two. Use references to achieve that.

Joins

In the above case no posts data is selected. Above query can also
produce duplicate result. To see it let’s create some sample data.

defself.setupUser.delete_allPost.delete_allu=User.createname:'Neeraj'u.posts.create!title:'ruby',desc:'ruby is awesome'u.posts.create!title:'rails',desc:'rails is awesome'u.posts.create!title:'JavaScript',desc:'JavaScript is awesome'u=User.createname:'Neil'u.posts.create!title:'JavaScript',desc:'Javascript is awesome'u=User.createname:'Trisha'end

With the above sample data if we execute User.joins(:posts) then this is the result we get