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

left_join work with join_table raise error #36742

Closed
archfish opened this issue Jul 24, 2019 · 0 comments · Fixed by #36776
Closed

left_join work with join_table raise error #36742

archfish opened this issue Jul 24, 2019 · 0 comments · Fixed by #36776

Comments

@archfish
Copy link

archfish commented Jul 24, 2019

Steps to reproduce

define

create_table :promotions, id: :uuid do |t|
  t.timestamps
end

create_table :products, id: :uuid do |t|
  t.timestamps
end

create_join_table :promotions, :products do |t|
  t.uuid :promotion_id, null:  false
  t.uuid :product_id, null:  false
end
class Promotion < ApplicationRecord
  has_and_belongs_to_many :products, join_table: :products_promotions
end
class Product < ApplicationRecord
  has_and_belongs_to_many :promotions, join_table: :products_promotions
end

use

# this not work
Promotion.left_joins(:products_products).where(products_promotions: {product_id: nil})

# ActiveRecord::ConfigurationError: Can't join 'Promotion' to association named 'products_products'; perhaps you misspelled it?

# this work
Promotion.left_joins(:promotions_products).where(products_promotions: {product_id: nil})

# Promotion Load (0.6ms)  SELECT  "promotions".* FROM "promotions" LEFT OUTER JOIN "products_promotions" ON "products_promotions"."promotion_id" = "promotions"."id" WHERE "products_promotions"."product_id" IS NULL LIMIT $1  [["LIMIT", 11]]

Expected behavior

left_joins should support join_table.

Actual behavior

left_joins raise error with join_table name.

System configuration

Rails version:
Rails 5.2.3

Ruby version:
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-darwin18]

Database version:
postgresql: stable 11.3 (bottled), HEAD

giraffate added a commit to giraffate/rails that referenced this issue Jul 26, 2019
In using HABTM, join middle table alias is combined with the associated
models name without sort, while middle table name is combined with those
models name with sort.

Fixes rails#36742.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants