Forked from wefarrell/Ransack habtm broken for postgres UUID
Last active
May 11, 2017 03:01
-
-
Save wjordan/2dc31ce894c1d6f4df9a716a87f64eb3 to your computer and use it in GitHub Desktop.
add shebang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
# Rails master | |
gem 'rails', '5.0.0' | |
gem 'sqlite3' | |
gem 'ransack' | |
gem 'activeuuid' | |
gem 'pg' | |
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