Limit preloading associations

I read Ecto docs https://hexdocs.pm/ecto/Ecto.Query.html#preload/3-preload-queries .It supports limit but not with preload associations.

I have customers withhas_many users. And I want to preload only certain number of users. Currently I am applying limit to the customers so only 10 customers will load. But I also want to apply limit to how many users will load with each customer.
Its because of pagination purposes

Any work around or suggestions?

Thanks

The two preload methods both allow you to have custom queries used for retrieving associations, which can have a limit. But I’d really suggest not using preloading for pagination. I’d rather look into a function API to load those paginated associations making use of:

assoc_query = Ecto.assoc(customer, :users)
query = from u in assoc_query, limit: 20
3 Likes