Run calculation on an has_many relation?

Is it possible to run a calculation on a calculation in another resource?

# resource user
attributes do
  attribute :type, :atom do
    constraints [one_of: [:m, :f, :mf]]
  end

  calculations do
    calculate :nr_of_people, :integer, expr(if sex == :mf do 2 else 1 end)
  end
end

# resource reservation
relationships do
  belongs_to :event, TimeTracer.Events.Event, allow_nil?: false, attribute_writable?: true
  belongs_to :user, TimeTracer.Accounts.User, allow_nil?: false
end

calculations do
  calculate :nr_of_guests, :integer do
    load :user
    calculation expr(user.nr_of_people)
  end
end

# in event resource
relationships do
  has_many :reservations, TimeTracer.Events.Reservation
end

calculations do
  calculate :nr_of_guests, :integer do
    # no idea? should be something like sum(reservations, :nr_of_guests) but that doesnt work
  end
end

What we call “inline aggregates” work roughly the way you’ve suggested, but the second argument is a keyword list:

calculations do
  calculate :nr_of_guests, :integer, expr(sum(reservations, field: :nr_of_guests))
end

Alternatively, in this case you could use a regular aggregate.

aggregates do
  sum :nr_of_guests, [:reservations], :nr_of_guests
end

Or express it a bit more directly going all the way to user:

aggregates do
  sum :nr_of_guests, [:reservations, :user], :nr_of_people
end

Ok, your last suggestion worked:

aggregates do
  sum :nr_of_guests, [:reservations, :user], :nr_of_people
end

The other ones did not work.

Interesting that the others didn’t work. Was there an error/stacktrace? Probably something I need to look into.