After showing and demonstrating code for demographic queries, the book looks ahead:
Now let’s apply the same approach to our product ratings query.
However, the product ratings query uses a constructor, not a reducer:
def with_user_ratings(user) do
base()
|> preload_user_ratings(user)
end
Compare that with the demographic query reducer on page 177:
def for_user(query \\ base(), user) do
query
|> where([d], d.user_id == ^user.id)
end
Other examples later in the book also use the reducer design:
page 242:
def with_average_ratings(query \\ base()) do
page 257:
def join_users(query \\ base()) do
def join_demographics(query \\ base()) do
def filter_by_age_group(query \\ base(), filter) do
page 266:
def with_zero_ratings(query \\ base()) do
I suggest that the book consistently use the reducer design or explain why the constructor design is preferred for the product ratings query.