class Article
schema articles do
type tags : Array(Tag), through: TagRelation, foreign_key: "article_id"
end
end
class Tag
schema tags do
type value : String
type articles : Array(Article), through: TagRelation, foreign_key: "tag_id"
end
end
class TagRelation
schema tag_relations do
type tag : Tag, key: "tag_id"
type article : Article, key: "article_id"
end
end
sql = <<-SQL
SELECT articles.*, tags.value
FROM articles
JOIN tag_relations ON tag_relations.article_id = articles.id
JOIN tags ON tags.id = tag_relations.tag_id
WHERE id = ?
SQL
Article.join(tags: true) { |x| x.select(:value) }.where(id: 42).build == {sql, [42]}