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_alias sometimes does not work correctly #1239

Open
itsalongstory opened this issue Jul 4, 2021 · 1 comment · May be fixed by #1512
Open

ransack_alias sometimes does not work correctly #1239

itsalongstory opened this issue Jul 4, 2021 · 1 comment · May be fixed by #1512

Comments

@itsalongstory
Copy link
Contributor

itsalongstory commented Jul 4, 2021

test_ransack.rb

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  gem 'activerecord', '~> 6.1', '>= 6.1.4', require: "active_record"
  gem 'sqlite3', '~> 1.4'
  gem 'ransack', '~> 2.4', '>= 2.4.2'
  gem 'minitest', '~> 5.14', '>= 5.14.2', require: "minitest/autorun"
end

ActiveRecord::Base.establish_connection(
  adapter:  "sqlite3",
  database: "./test_ransack_alias"
)

ActiveRecord::Schema.define do
  drop_table(:fees, if_exists: true)

  create_table :fees do |t|
    t.integer :amount
  end
end


class Fee < ActiveRecord::Base
  ransack_alias :amount_a, :amount
end

class MyTest < Minitest::Test
  # success
  def test_1
    query_params = { amount_not_eq: 1 }
    assert_equal "SELECT \"fees\".* FROM \"fees\" WHERE \"fees\".\"amount\" != 1", Fee.ransack(query_params).result.to_sql
  end

  # success
  def test_2
    query_params = { amount_not_eq: 1, amount_a_not_eq: 2 }
    assert_equal "SELECT \"fees\".* FROM \"fees\" WHERE (\"fees\".\"amount\" != 1 AND \"fees\".\"amount\" != 2)", Fee.ransack(query_params).result.to_sql
  end

  # failure
  def test_3
    query_params = { amount_a_not_eq: 2, amount_not_eq: 1 }
    assert_equal "SELECT \"fees\".* FROM \"fees\" WHERE (\"fees\".\"amount\" != 2 AND \"fees\".\"amount\" != 1)", Fee.ransack(query_params).result.to_sql
  end
end
deploy@local-vm:~$ ruby test_ransack.rb
Fetching gem metadata from https://gems.ruby-china.com/........
Resolving dependencies...
Using concurrent-ruby 1.1.9
Using i18n 1.8.10
Using minitest 5.14.4
Using tzinfo 2.0.4
Using zeitwerk 2.4.2
Using activesupport 6.1.4
Using activemodel 6.1.4
Using activerecord 6.1.4
Using bundler 2.2.19
Using ransack 2.4.2
Using sqlite3 1.4.2
-- drop_table(:fees, {:if_exists=>true})
   -> 0.0094s
-- create_table(:fees)
   -> 0.0052s
Run options: --seed 9735

# Running:

..F

Finished in 0.008001s, 374.9565 runs/s, 374.9565 assertions/s.

  1) Failure:
MyTest#test_3 [test_ransack.rb:45]:
--- expected
+++ actual
@@ -1 +1 @@
-"SELECT \"fees\".* FROM \"fees\" WHERE (\"fees\".\"amount\" != 2 AND \"fees\".\"amount\" != 1)"
+"SELECT \"fees\".* FROM \"fees\" WHERE \"fees\".\"amount\" != 1"


3 runs, 3 assertions, 1 failures, 0 errors, 0 skips
itsalongstory added a commit to itsalongstory/ransack that referenced this issue Aug 1, 2024
@itsalongstory itsalongstory linked a pull request Aug 1, 2024 that will close this issue
@itsalongstory
Copy link
Contributor Author

gem 'ransack', github: 'itsalongstory/ransack', branch: 'issue_1239'

Fixed this.

itsalongstory added a commit to itsalongstory/ransack that referenced this issue Aug 12, 2024
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 a pull request may close this issue.

1 participant