How to Determine if Rails Association is Eager Loaded?

前端 未结 7 1298
后悔当初
后悔当初 2021-01-30 16:02

Does anyone know a way to determine if a Rails association has been eager loaded?

My situation: I have a result set where sometimes one of the associations is eager l

7条回答
  •  一整个雨季
    2021-01-30 16:38

    item.shipping_infos.loaded? will tell you.

    I gotta say, though: this path leads to madness... before writing code that tests loaded? to decide between #detect and #find, make sure this instance really matters, relative to everything else that's going on.

    If this isn't the slowest thing your app does, adding extra code paths adds unnecessary complexity. Just because you might waste a little database effort doesn't mean you need to fix it - it probably doesn't matter in any measurable way.

提交回复
热议问题