首页 > 解决方案 > Ecto query unique through a belongs_to association?

问题描述

I have two models Event and Customer. The relationship is that an Event belongs_to a Customer, which can has_many Events.

Now, I need to return the count of unique Customers by way of Events, which fall in a particular date range. Does Ecto have any query method that can do what I need?

标签: elixirecto

解决方案


我设法做到了这一点,而不必预先加载或将客户加入事件,因为我可以使用 distinct: event.customer_id


推荐阅读