Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Through references #83

Open
vladfaust opened this issue Mar 14, 2019 · 0 comments
Open

Through references #83

vladfaust opened this issue Mar 14, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@vladfaust
Copy link
Member

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]}
@vladfaust vladfaust added the enhancement New feature or request label Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant