Many To Many Relationship Example

After reading https://www.ash-hq.org/docs/guides/ash/latest/topics/relationships.md#has-many and https://ash-hq.org/docs/dsl/ash/latest/ash-resource-dsl/relationships/many_to_many I am a little bit confused since the examples use different code. I am looking for a simple example which I can copy and paste. Something like a blog post which has multiple tags associated via a many to many relationship:

defmodule Example.Blog.Post do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

  attributes do
    uuid_primary_key :id

    attribute :content, :string do
      allow_nil? false
    end
  end
  
  postgres do
    table "blog_posts"
    repo Example.Repo
  end
  
  actions do
    defaults [:create, :read]
  end
end


defmodule Example.Blog.Tag do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

  attributes do
    uuid_primary_key :id

    attribute :name, :string do
      allow_nil? false
    end
  end
  
  postgres do
    table "tags"
    repo Example.Repo
  end
  
  actions do
    defaults [:create, :read]
  end
end


- What do I have to change in the existing files?
- How do I have to do the third resource?
- How would I add a tag to a blog post the Ash way?
Was this page helpful?