I've got two models, Song and Vote, where songs has many votes. I want to select all songs and count the number of votes for each.
The index action in the SongController, generated using the mix gen task, has been modified to this:
def index(conn, _params) do
query = from s in Song, select: %{id: s.id, name: s.name, artist: s.artist}
songs = Repo.all(query)
render(conn, "index.html", songs: songs)
end
In this case songs
contains a list of lists. But in the orginal, generated function, songs = Repo.all(Song)
it is a list of Song structs.
This means that the song_path functions in the template break with the following error message: maps cannot be converted to_param. A struct was expected, got: %{artist: "Stephen", id: 3, name: "Crossfire"}
Of course, what I really want to do is to somehow add a num_votes
field to the select statement, and then somehow make a corresponding field to the Song struct?