Skip to content

Conversation

osyo-manga
Copy link
Collaborator

Fixed by Does not respect table alias on join clause #24

before

class Company < ActiveRecord::Base
  include ActiveRecord::Bitemporal
  has_many  :employees,  foreign_key: :company_id, autosave: true
end

class Employee < ActiveRecord::Base
  include ActiveRecord::Bitemporal
  belongs_to :company
end

p Company.joins(:employees).left_joins(:employees).where(employees: { bitemporal_id: employee.id }).count
# => 3

puts Company.joins(:employees).left_joins(:employees).where(employees: { bitemporal_id: employee.id }).to_sql
# => SELECT "companies".*
#      FROM "companies"
#     INNER JOIN "employees"
#        ON "employees"."company_id"           =  "companies"."bitemporal_id"
#       AND "employees"."valid_from"           <= '2019-05-13 08:40:40.496312'
#       AND "employees"."valid_to"             >  '2019-05-13 08:40:40.496312'
#       AND "employees"."deleted_at" IS NULL
#      LEFT OUTER JOIN "employees" "employees_companies"
#        ON "employees_companies"."company_id" =  "companies"."bitemporal_id"
#       AND "employees"."valid_from"           <= '2019-05-13 08:40:40.496426'
#       AND "employees"."valid_to"             >  '2019-05-13 08:40:40.496426'
#       AND "employees"."deleted_at" IS NULL
#     WHERE "companies"."valid_from"           <= '2019-05-13 08:40:40.496468'
#       AND "companies"."valid_to"             >  '2019-05-13 08:40:40.496468'
#       AND "companies"."deleted_at" IS NULL
#       AND "employees"."bitemporal_id"        =  1

after

class Company < ActiveRecord::Base
  include ActiveRecord::Bitemporal
  has_many  :employees,  foreign_key: :company_id, autosave: true
end

class Employee < ActiveRecord::Base
  include ActiveRecord::Bitemporal
  belongs_to :company
end

p Company.joins(:employees).left_joins(:employees).where(employees: { bitemporal_id: employee.id }).count
# => 1

puts Company.joins(:employees).left_joins(:employees).where(employees: { bitemporal_id: employee.id }).to_sql
# => SELECT "companies".*
#      FROM "companies"
#     INNER JOIN "employees"
#        ON "employees"."company_id"           =  "companies"."bitemporal_id"
#       AND "employees"."valid_from"           <= '2019-05-13 08:39:50.504885'
#       AND "employees"."valid_to"             >  '2019-05-13 08:39:50.504885'
#       AND "employees"."deleted_at" IS NULL
#      LEFT OUTER JOIN "employees" "employees_companies"
#        ON "employees_companies"."company_id" =  "companies"."bitemporal_id"
#       AND "employees_companies"."valid_from" <= '2019-05-13 08:39:50.504984'
#       AND "employees_companies"."valid_to"   >  '2019-05-13 08:39:50.504984'
#       AND "employees_companies"."deleted_at" IS NULL
#     WHERE "companies"."valid_from"           <= '2019-05-13 08:39:50.505018'
#       AND "companies"."valid_to"             >  '2019-05-13 08:39:50.505018'
#       AND "companies"."deleted_at" IS NULL
#       AND "employees"."bitemporal_id"        =  1

@auto-assign auto-assign bot requested review from marumarism and meganemura May 13, 2019 08:42
@osyo-manga osyo-manga requested review from f440 and removed request for marumarism May 13, 2019 08:42
Copy link
Member

@meganemura meganemura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

Copy link
Member

@f440 f440 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@osyo-manga
Copy link
Collaborator Author

@meganemura @f440 Thanks! :)

@osyo-manga osyo-manga merged commit 8d43539 into kufu:master May 13, 2019
@osyo-manga osyo-manga deleted the fix-24 branch May 13, 2019 08:48
@osyo-manga osyo-manga mentioned this pull request May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants