Steve
Steve

Reputation: 391

Display Results from %Mariaex{} Map

I had to use the Ecto.Adapters.SQL.query!/4 to query my database. It worked great and returns the results I was expecting.

When I try to display the results on a Phoenix page using <%= for result <- @results do %> I get this error

protocol Enumerable not implemented for %Mariaex.Result{...}

So the question is, how do I enumerate over the results to display them in an HTML table?


Dogbert got me onto the right track but I ultimately ended up doing this so that I can have individual control of each table cell (e.g. assign classes, attributes, etc.).

<%= for row <- @results.rows do %>
<% [field_1, field_2, field_3, field_4, field_5, _, _, _] = row  %>
<tr>
  <td nowrap><%= field_1 %></td>
  <td class="fancy><%= field_2 %></td>
...

Upvotes: 0

Views: 97

Answers (1)

Dogbert
Dogbert

Reputation: 222198

According to the README, the fields' data is stored in the rows field of the struct and the name of the columns are in columns. To convert these into a key/value pair you can use Enum.zip. This should work:

<%= for row <- @results.rows do %>
  <%= for {column, value} <- Enum.zip(@results.columns, row) do %>
    <%= inspect {column, value} %>
  <% end %>
<% end %>

PS: I'd rename the variable to @result.

Upvotes: 1

Related Questions