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

前端 未结 6 1782
名媛妹妹
名媛妹妹 2020-12-15 18:37

What\'s the difference between \"includes\" and \"joins\" in ActiveRecord query? Can anyone explain to me with the following two associated

6条回答
  •  天命终不由人
    2020-12-15 18:42

    Joins will just join the tables and bringsselected fields in return. if you call associations on joins query result, it will fire database queries again

    Include will eager load the included associations and add them in memory. Include loads all the included tables attributes. If you call associations on include query result, it wont fire any queries

    You can find a detailed explanation with examples in this article of mine: Active Record Associations Tips & Tricks.

提交回复
热议问题