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

Ransack is not correctly overriding/using previous joins #824

Open
omitter opened this issue Aug 4, 2017 · 2 comments
Open

Ransack is not correctly overriding/using previous joins #824

omitter opened this issue Aug 4, 2017 · 2 comments

Comments

@omitter
Copy link

omitter commented Aug 4, 2017

Joining fails when using multiple search patterns at once, when joining a table that ransack wants to join later for searching.

Having these aliases in the Order model (Order belongs to Collection and Customer (User) through CustomerRelation)

ransack_alias :uname, :customer_relation_customer_name
ransack_alias :cname, :collection_name

Running the following ransack generated queries, sql fails:

Order.joins(:collection).ransack({:uname_cont=>"foo", :cname_cont=>"bar"}).result.page(1).to_a
#=> ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'collections_orders.name' in 'where clause':
 SELECT  `orders`.* 
FROM `orders` 
INNER JOIN `collections` ON `collections`.`id` = `orders`.`collection_id`
 LEFT OUTER JOIN `customer_relations` ON `customer_relations`.`id` = `orders`.`customer_relation_id`
 LEFT OUTER JOIN `customers` ON `customers`.`id` = `customer_relations`.`customer_id` 
WHERE (`customers`.`name` LIKE '%foo%' AND `collections_orders`.`name` LIKE '%bar%')
 LIMIT 25 OFFSET 0
# from /usr/local/rvm/gems/ruby-2.4.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:120:in `_query'

Order.left_outer_joins(:collection).ransack({:uname_cont=>"foo",cname_cont=>"bar"}).result.page(1).to_a
#=> ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias: 'collections': 
SELECT  `orders`.* 
FROM `orders`
 LEFT OUTER JOIN `customer_relations` ON `customer_relations`.`id` = `orders`.`customer_relation_id`
 LEFT OUTER JOIN `customers` ON `customers`.`id` = `customer_relations`.`customer_id`
 LEFT OUTER JOIN `collections` ON `collections`.`id` = `orders`.`collection_id` 
LEFT OUTER JOIN `collections` ON `collections`.`id` = `orders`.`collection_id` 
WHERE (`customers`.`name` LIKE '%foo%' AND `collections`.`name` LIKE '%bar%') 
LIMIT 25 OFFSET 0

Order.left_outer_joins(:collection).ransack({cname_cont: 'bar'}).result.page(1).to_a
#=> ActiveRecord::StatementInvalid: Mysql2::Error: Not unique table/alias: 'collections': 
SELECT  `orders`.* FROM `orders` 
LEFT OUTER JOIN `collections` ON `collections`.`id` = `orders`.`collection_id`
 LEFT OUTER JOIN `collections` ON `collections`.`id` = `orders`.`collection_id`
 WHERE (`collections`.`name` LIKE '%bar%') 
LIMIT 25 OFFSET 0
# from /usr/local/rvm/gems/ruby-2.4.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:120:in `_query'

but this does miraculously work (as it should)

Order.joins(:collection).ransack({cname_cont: 'bar'}).result.page(1).to_a
  Order Load (0.9ms)  SELECT  `orders`.* FROM `orders` INNER JOIN `collections` ON `collections`.`id` = `orders`.`collection_id` WHERE (`collections`.`name` LIKE '%bar%') LIMIT 25 OFFSET 0

as does this (changing the hash order of the initial example⁉)

Order.joins(:collection).ransack({cname_cont: 'bar', uname_cont: 'foo'}).result.page(1).to_a
  Order Load (1.4ms)  SELECT  `orders`.* FROM `orders` INNER JOIN `collections` ON `collections`.`id` = `orders`.`collection_id` LEFT OUTER JOIN `customer_relations` ON `customer_relations`.`id` = `orders`.`customer_relation_id` LEFT OUTER JOIN `customers` ON `customers`.`id` = `customer_relations`.`customer_id` WHERE (`collections`.`name` LIKE '%bar%' AND `customers`.`name` LIKE '%foo%') LIMIT 25 OFFSET 0

So left_outer_joins does never work and joins works depending on the hash order ransack receives… confusing.

@wuboy0307
Copy link

Got the same error, I believe it related to this issue.
#374

Alias doesn't properly apply.

@zephyrpathsofglory
Copy link

I got the same error, the hash order ransack receives differs the result!

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

3 participants