Skip to content

Commit

Permalink
add distinct to example
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Aug 12, 2024
1 parent a7238cf commit c808d21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class Book < ActiveRecord::Base

# prefacers for the book via a join table
has_many :forewords
has_many :foreworders, through: :forewords, source: :user
has_many :foreworders, -> { distinct }, through: :forewords, source: :user

# illustrators for the book via a join table
has_many :illustrations
has_many :illustrators, through: :illustrations, source: :user
has_many :illustrators, -> { distinct }, through: :illustrations, source: :user

# editors for the book via a join table
has_many :edits
Expand Down Expand Up @@ -121,16 +121,16 @@ book.contributors.where(id: editor.id)
# => [#<User id=2, name="John W. Campbell">]

book.contributors.to_sql
# => SELECT * FROM users WHERE id IN (
# => SELECT DISTINCT * FROM users WHERE id IN (
# SELECT id FROM users WHERE id = 1
# UNION
# SELECT users.id FROM users INNER JOIN prefaces ON users.id = prefaces.user_id WHERE prefaces.book_id = 1
# UNION
# SELECT users.id FROM users INNER JOIN forewords ON users.id = forewords.user_id WHERE forewords.book_id = 1
# UNION
# SELECT users.id FROM users INNER JOIN illustrations ON users.id = illustrations.user_id WHERE illustrations.book_id = 1
# SELECT DISTINCT users.id FROM users INNER JOIN illustrations ON users.id = illustrations.user_id WHERE illustrations.book_id = 1
# UNION
# SELECT users.id FROM users INNER JOIN edits ON users.id = edits.user_id WHERE edits.book_id = 1
# SELECT DISTINCT users.id FROM users INNER JOIN edits ON users.id = edits.user_id WHERE edits.book_id = 1
# )

# example of more advanced querying e.g. preloading the union
Expand Down
8 changes: 4 additions & 4 deletions spec/union_of_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1391,10 +1391,10 @@ def owned = where(owner: proxy_association.owner)
has_many :foreworders, through: :forewords, source: :user

has_many :illustrations
has_many :illustrators, through: :illustrations, source: :user
has_many :illustrators, -> { distinct }, through: :illustrations, source: :user

has_many :edits
has_many :editors, through: :edits, source: :user
has_many :editors, -> { distinct }, through: :edits, source: :user

has_many :contributors, -> { distinct }, class_name: 'User', union_of: %i[
author
Expand Down Expand Up @@ -1485,7 +1485,7 @@ def owned = where(owner: proxy_association.owner)
)
UNION
(
SELECT
SELECT DISTINCT
users.id
FROM
users INNER JOIN illustrations ON users.id = illustrations.user_id
Expand All @@ -1494,7 +1494,7 @@ def owned = where(owner: proxy_association.owner)
)
UNION
(
SELECT
SELECT DISTINCT
users.id
FROM
users INNER JOIN edits ON users.id = edits.user_id
Expand Down

0 comments on commit c808d21

Please sign in to comment.