lapinkoira
Hi all, I am reading the Programming Phoenix book and following the steps to implement the Rumbl webapp.
Then at some steps I try to add something custom to learn a bit more.
So in that application, I have a controller which renders a template like this:
Listing videos
<%= for video <- @videos do %> <td class="text-right">
<%= link "Show", to: video_path(@conn, :show, video), class: "btn btn-default btn-xs" %>
<%= link "Edit", to: video_path(@conn, :edit, video), class: "btn btn-default btn-xs" %>
<%= link "Delete", to: video_path(@conn, :delete, video), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %>
</td>
</tr>
<% end %>
| User | Url | Title | Description | Category | |
|---|---|---|---|---|---|
| <%= video.user_id %> | <%= video.url %> | <%= video.title %> | <%= video.description %> | <%= if category = video.category, do: category.name %> |
<%= link “New video”, to: video_path(@conn, :new) %>
I added this line to render the categories <%= if category = video.category, do: category.name %>
This is my controller
def index(conn, _params, user) do
videos = Repo.all(user_videos(user)) |> Repo.preload(:category)
render(conn, “index.html”, videos: videos)
end
defp user_videos(user) do
# query user videos
assoc(user, :videos)
end
And when I run a test I wrote, this one:
test “renders index.html”, %{conn: conn} do
videos = [%Rumbl.Video{id: “1”, title: “dogs”},
%Rumbl.Video{id: “2”, title: “cats”}]
content = render_to_string(Rumbl.VideoView, “index.html”,
conn: conn, videos: videos)
assert String.contains?(content, "Listing videos")
for video <- videos do
assert String.contains?(content, video.title)
end
end
It complains with categories not being preloaded
** (KeyError) key :name not found in: #Ecto.Association.NotLoaded
I also tried this:
defp user_videos(user) do
query = (from u in user, select: u.videos, preload: [:category])
Repo.all(query)
end
But not working neither, so how can I preload them?
I thought I just needed to preload them at the pipe with |> Repo.preload(:category)
Also the strange thing is this started to crash with test, without the test it renders the index.html template without any issue
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dom
It doesn’t matter what you do in the controller, since this test tests the view, not the controller.
You need to preload the category for these videos (in the test itself). Here that could mean just hardcoding it like:
lapinkoira
Thanks that works, is there a way to make it more elegant handling it at the template? like “if video.category is nil dont ask for it”
jc00ke
I’m wondering this as well. It’s becoming a bear to preload associations. I’m close to writing a
assert_without_assocfunctionLostKobrakai
There’s
Ecto.assoc_loaded?jc00ke
Thanks, but that’s not quite what I’m looking for.
This is more of what I’m looking for (or at least what I think I want
)
LostKobrakai
Maybe this can shed some light:
jc00ke
Word, that makes sense, except that’s not how the test code from
mix phx.gen.html|contextis generated. In a pinch I compare ids, but that doesn’t feel right.LostKobrakai
That code is meant as a starting point to get people going and not to be some holy grail of how things are to be done. Their focus lies quite a bit more on being accessable to people new to the framework.
That I talk about in the linked post: Compare IDs if you want to assert on the identity (Is it the same thing?). Compare fields to each other if you want to make sure creating / updating works correctly for them.
axelson
I have a little helper function that might be useful to you:
With that you could write a
assert_matches_without_associationsfunction. Just callclear_associations/1on each input and then assert equality.Although (in line with what @LostKobrakai is saying) I actually use this
assert_ids_match/2helper more:jc00ke
Awesome, thanks for the code! I’ll probably head the
assert_ids_matchroute soon; it’s good enough for me.