I have a has_one
and belongs_to
relationship between students
and enrollment
. The enrollment belongs to a student.
In an index view I am returning a list of students, preloading the enrollment and outputting columns to sort the data on out to a table. This all works fine.
One column I have in the outputted view is for_grade
which is in the Enrollment
schema. I need to be able to order_by
this column but I can’t work out how to do this?
I have utilized the guide in What’s New In Ecto 2.1 for filtering on params using the following technique
def filter(params) do
Student
|> order_by(^filter_order_by(params["order_by"]))
end
defp filter_order_by("name_desc"), do: [desc: :last_name]
defp filter_order_by("name"), do: [asc: :last_name]
defp filter_order_by("received_desc"), do: [desc: :inserted_at]
defp filter_order_by("received"), do: [asc: :inserted_at]
...
Am I missing something completely obvious? Is there a idiomatic/recognized way to order_by
on the association which I’m overlooking?
Thanks in advance.