Lazy preloading only some columns of relations in Rails

84 Views Asked by At

I'm currently using ar_lazy_preload Gem, for lazy preload, but sometimes I only need some attributes of those relations preloaded, I was wondering if there is a way to retrieve only some columns using select, maybe using Arel? (I'm not very familiar with it)

An example: User - Person: 1 to 1 relation

User.lazy_preload(:person).all , but from person relation only need date of birth

the closest issue i found was this it does not work with ar_lazy_preload

I expect to lazy preload but with a query that uses a select to retrieve only what i need

users = User.lazy_preload(person: [:date_of_birth]).all making 1 queries

Select * from users users.first.person.date_of_birth makes another query

Select people.date_of_birth from people where people.user_id in ($ids from first query)

0

There are 0 best solutions below