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

Invalid table alias when searching has_one through associations #1227

Closed
mbiang opened this issue May 20, 2021 · 1 comment
Closed

Invalid table alias when searching has_one through associations #1227

mbiang opened this issue May 20, 2021 · 1 comment

Comments

@mbiang
Copy link

mbiang commented May 20, 2021

When searching a 'has_one thought' association, a invalid table alias is used in the SQL query if a search parameter for the cousin object is passed in the params has prior to the parameter for the sibling object.

Here is an example:

class A < ActiveRecord::Base
  belongs_to :b, optional: true
  has_one :c, through: :b
end

class B < ActiveRecord::Base
  has_many :as
  belongs_to :c
end

class C < ActiveRecord::Base
  has_many :bs, dependent: :destroy
  has_many :as, through: :b
end

# the following call will succeed
A.search({b_name_equals: 'B', c_name_equals: 'C'}).result

# the following call will fail with this postgres error:
# ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "bs_as")
# LINE 1: ..."."id" = "bs"."c_id" WHERE ("cs"."name" = 'C' AND "bs_as"."n..

A.search({c_name_equals: 'C', b_name_equals: 'B'}).result


Apparently, when building the query, it first joins on "b" in order to join on "cs" to search c.name, and then it it incorrectly assumes the table alias for "bs" as "bs_as", when it should just be "bs".

Ruby Version: 2.7.3
Rails Version: 6.1.3.2
Ransack Version: 2.4.2
Postgres Version: 10.4
Active Admin Version: 2.9.0

@mbiang mbiang changed the title Invalid table alias when searching has_many through associations Invalid table alias when searching has_one through associations May 20, 2021
@deivid-rodriguez
Copy link
Contributor

deivid-rodriguez commented Oct 23, 2023

I'm going to close this since #1447 is likely to have fixed this. Feel free to reopen if not though!

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

No branches or pull requests

2 participants