Format form option: view function or with select in query

I have two different forms in my application that take a list of options for a select drop down. I generate the options from a database query.

I’ve discovered that for one form, I pass the entire struct to my view and use a view function to format the structs into options.

  def format_players_as_options(players) do
    Enum.map(players, &format_player_as_option(&1))
  end

  defp format_player_as_option(player), do: {player.player_name, player.id}

For the other I use a select in my query to format query results for the select option.

select: %{player_name: p.player_name, id: p.id},

I’d like to standardize my approach in the app. Is one approach better than the other? Using select in the query eliminates the need for the view function, but the query is less reusable since it returns very specific data.

I refine the select part of the query as that then means less data is transferred from the database too, thus making it in general faster as well. :slight_smile:

1 Like