many_to_many with non-default attribute not preloading...
So, I have a many to many relationship I'm strugling with. It must be something silly, but I just can't make it work.
I have a resource called
Game
, which is actually for a table called assets
, so in the join table one of the attributes is asset_id
and not game_id
.
The join table is called playlist_entries
Any help would be greatly appreciated. Basically it is not loading the games correctly for the playlist. Addng the full resources made the post too long, so hopefully this will shed enough light...
Playlist
PlaylistEntry
Game
10 Replies
when you say you can't get it to work, what is going wrong?
The associated entries are not there for some reason

What does that read action look like on playlist?
Are the join records there looking like you’d expect?
Also try updating to latest release candidate of ash
this is the read action:
I'll see if updating helps
If I add this:
And load this instead of the
:games
it does preload the join table correctly
so upgrading didn't help.
When I load it like this: |> Ash.Query.load([playlist_entries: :game])
the game is nil
and if I change the naming to asset
which is the actual table/field name, it is still nil. Is there anything specific I need to set in the join resource?Oh, know what
Nah nvm
It’s something to do with the attributes/relationships defined. How does the data look in the database?
Does it look right? Right values in the right columns? Double check the ids/foreign keys
yeah the data and tables are all correct. It is already used in an existing system. I'm pretty sure it must be something dumb. Let me share the full resource files. Maybe you can spot it.
Here is a gist with the full resource modules: https://gist.github.com/drumusician/3d831de382ee480e2f5f161429fb93ca
The only thing I can think of is that it breaks because I am calling the
asset
a game
, but that should be possible by defining the attributes correctly.
Thanks a lot for your help!Gist
Many to Many with Ash
Many to Many with Ash. GitHub Gist: instantly share code, notes, and snippets.
I really don't think that would be the problem 🙂
This is your problem
The primary read is used to load the relationship
so thats filtering
id == nil
I should probably make it warn or raise if get? true
is used on the primary read
If you do defaults [:read]
to use the default read implementation of a primary read, and call your action something else (like :by_id
) then it should resolve your issueThanks a lot @Zach Daniel that was indeed the problem. Works like a charm. I'll try to remember to be careful with
get? true
ok, one last question. The join table for this relationship has a position field. How do I go about sorting based on that field?Do you mean you want each related value sorted by its position?
That might actually be harder than it should be 😓 there are definitely ways to do it
Are you also applying a limit to the number of related records?
Probably the easiest way to do what you want to do is to actually go through the underlying
has_many
relationship.
Then if you run that action, you'll have playlist.playlist_entries # [list, sorted, by, position]
and each playlist entry will have it's game
The other way, a bit harder, is to build a manual relationshipok, cool. Thanks a lot again for the elaborate examples! Really appreciate all the effort you put into this!
I figured it wasn't going to be straightforward, but this makes total sense. 😎