Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wefarrell/d0c16e5eb372bc7365a94f6b11261f74 to your computer and use it in GitHub Desktop.
Save wefarrell/d0c16e5eb372bc7365a94f6b11261f74 to your computer and use it in GitHub Desktop.
# test-ransacker-arel-present-predicate.rb
# Run it in your console with: `ruby test-ransacker-arel-present-predicate.rb`
# If you change the gem dependencies, run it with:
# `rm gemfile* && ruby test-ransacker-arel-present-predicate.rb`
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
# Rails master
gem 'rails', '5.0.0'
gem 'sqlite3'
gem 'ransack'
gem 'activeuuid'
gem 'pg'
GEMFILE
system 'bundle install'
end
require 'bundler'
Bundler.setup(:default)
require 'minitest/autorun'
require 'logger'
require 'active_record'
require 'ransack'
database = YAML.load File.read('database.yml')
puts database['development']
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(database['development'])
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
break if ActiveRecord::Base.connection.data_source_exists? :students
enable_extension "uuid-ossp"
create_table :students, id: :uuid, default: -> { "uuid_generate_v4()" } do |t|
t.string :name
end
create_table :groups do |t|
t.string :name
end
create_table :teachers do |t|
t.string :name
end
create_join_table :groups, :students
create_join_table :groups, :teachers
end
class Student < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Teacher < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_ransackers
result_sql = Student.ransack({groups_id_eq: 2}).result.to_sql
expected_sql = 'SELECT "students".* FROM "students" LEFT OUTER JOIN "groups_students" ON "groups_students"."student_id" = "students"."id" LEFT OUTER JOIN "groups" ON "groups"."id" = "groups_students"."group_id" WHERE "groups"."id" = 2'
#This fails
assert_equal expected_sql, result_sql
result_sql = Teacher.ransack({groups_id_eq: 2}).result.to_sql
expected_sql = 'SELECT "teachers".* FROM "teachers" LEFT OUTER JOIN "groups_teachers" ON "groups_teachers"."teacher_id" = "teachers"."id" LEFT OUTER JOIN "groups" ON "groups"."id" = "groups_teachers"."group_id" WHERE "groups"."id" = 2'
#This passes
assert_equal expected_sql, result_sql
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment