stores = Store.joins(:car)
This will return all stores for which there is a car. stores[0].car
will result in another query.
stores = Store.includes(:car)
This will return all stores, car or no car. stores[0].car
will not result in another query.
stores = Store.includes(:car).joins(:car)
This will return all stores with a car. stores[0].car
will not result in another query. I wouldn't recommend this for has_many
relationships, but it works great for has_one
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…