How to Determine if Rails Association is Eager Loaded?

前端 未结 7 1294
后悔当初
后悔当初 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:45

    You can detect whether or not a single association has been loaded with loaded_foo?. For example, if shipping_info was a belongs_to association, then item.loaded_shipping_info? will return true when it's been eager-loaded. Oddly, it appears to return nil (rather than false) when it hasn't been loaded (in Rails 2.3.10 anyway).

提交回复
热议问题