Eager loading an association of a singular association from an instantiated ActiveRecord object in Rails 5

If you want to eager load an association of a singular association for an already-instantiated record, you can do this:

user.association(:child).target = user.association(:child).scope.eager_load(:toy).first

This is similar to one of the approaches you listed at the top of your question. In particular, notice the .target part.

ActiveRecord isn't optimized for this particular scenario, so the code is pretty ugly. For that reason, I would strongly lean toward your :child_with_toy approach if you really need to save the query.


It can be done easier with Preloader:

ActiveRecord::Associations::Preloader.new.preload(user, child: [:toy])