Limit preloading associations

I read Ecto docs .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?


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