AE
Ash Elixir•2w ago
Failz

receiving an Ash Error and don't understand why

question about this error. I'm trying to setup something similar in my app that the Tunez book does. Following an artist, but I'm wanting to like a post. Here's the code
# post.ex (resource)
has_many :like_relationships, App.Elevate.PostLike

many_to_many :likes, App.Accounts.User do
join_relationship :like_relationships
destination_attribute_on_join_resource :user_id
end

# post_like.ex (resource)
belongs_to :post, App.Elevate.Post do
allow_nil? false
primary_key? true
end

belongs_to :user, App.Accounts.User do
allow_nil? false
primary_key? true
end

# user.ex (resource)

has_many :like_relationships, App.Elevate.PostLike do
destination_attribute :user_id
end

many_to_many :liked_posts, App.Elevate.Post do
join_relationship :like_relationships
source_attribute_on_join_resource :user_id
end
# post.ex (resource)
has_many :like_relationships, App.Elevate.PostLike

many_to_many :likes, App.Accounts.User do
join_relationship :like_relationships
destination_attribute_on_join_resource :user_id
end

# post_like.ex (resource)
belongs_to :post, App.Elevate.Post do
allow_nil? false
primary_key? true
end

belongs_to :user, App.Accounts.User do
allow_nil? false
primary_key? true
end

# user.ex (resource)

has_many :like_relationships, App.Elevate.PostLike do
destination_attribute :user_id
end

many_to_many :liked_posts, App.Elevate.Post do
join_relationship :like_relationships
source_attribute_on_join_resource :user_id
end
The attached message shows the queries to start to fire off but then runs into an Ash exception.
2 Replies
ZachDaniel
ZachDaniel•2w ago
Hummmmm At first glance that looks right to me Maybe silly, but try replacing :user_id with :something_random one at a time until it shows up in the error message, then you know which one is the problem 🙂
ken-kost
ken-kost•2w ago
@Failz try maybe this 👀
# post.ex (resource)
has_many :post_likes, App.Elevate.PostLike

many_to_many :likes, App.Accounts.User do
through App.Elevate.PostLike
source_attribute_on_join_resource: :post_id
destination_attribute_on_join_resource :user_id
end

# post_like.ex (resource)
belongs_to :post, App.Elevate.Post do
allow_nil? false
primary_key? true
end

belongs_to :user, App.Accounts.User do
allow_nil? false
primary_key? true
end

# user.ex (resource)
has_many :post_likes, App.Elevate.PostLike

many_to_many :liked_posts, App.Elevate.Post do
through App.Elevate.PostLike
source_attribute_on_join_resource :user_id
destination_attribute_on_join_resource: :post_id
end
# post.ex (resource)
has_many :post_likes, App.Elevate.PostLike

many_to_many :likes, App.Accounts.User do
through App.Elevate.PostLike
source_attribute_on_join_resource: :post_id
destination_attribute_on_join_resource :user_id
end

# post_like.ex (resource)
belongs_to :post, App.Elevate.Post do
allow_nil? false
primary_key? true
end

belongs_to :user, App.Accounts.User do
allow_nil? false
primary_key? true
end

# user.ex (resource)
has_many :post_likes, App.Elevate.PostLike

many_to_many :liked_posts, App.Elevate.Post do
through App.Elevate.PostLike
source_attribute_on_join_resource :user_id
destination_attribute_on_join_resource: :post_id
end
I think :liked_posts used like_relationships destination attribute :user_id which made App.Elevate.Post that it doesn't know that reference

Did you find this page helpful?