Last active
July 30, 2016 13:45
-
-
Save jonatack/cd201e6817221b214d9b to your computer and use it in GitHub Desktop.
test-ransack-issue-608-order-of-predicate-arguments
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
# test-ransack-issue-608-order-of-predicate-arguments.rb | |
# This is a stand-alone test case. | |
# Run it in your console with: `ruby test-ransack-issue-608-order-of-predicate-arguments.rb` | |
# If you change the gem dependencies, run it with: | |
# `rm gemfile* && ruby test-ransack-issue-608-order-of-predicate-arguments.rb` | |
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
# Rails master | |
gem 'rails', github: 'rails/rails' | |
# Rails last release | |
# gem 'rails' | |
gem 'sqlite3' | |
gem 'ransack', github: 'activerecord-hackery/ransack' | |
gem 'polyamorous', github: 'activerecord-hackery/polyamorous' | |
GEMFILE | |
system 'bundle install' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require "active_record" | |
require "ransack" | |
require "sqlite3" | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
# Display versions. | |
message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{ | |
::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{ | |
::ActiveRecord::Base.connection.adapter_name}" | |
line = '=' * message.length | |
puts line, message, line | |
ActiveRecord::Migration.verbose = false | |
ActiveRecord::Schema.define do | |
create_table :authors, force: true do |t| | |
end | |
create_table :authorships, force: true do |t| | |
t.integer :author_id | |
t.integer :post_id | |
end | |
create_table :comments, force: true do |t| | |
t.integer :post_id | |
t.datetime :created_at | |
end | |
create_table :posts, force: true do |t| | |
end | |
end | |
class Author < ActiveRecord::Base | |
has_many :authorships | |
has_many :posts, through: :authorships | |
end | |
class Authorship < ActiveRecord::Base | |
belongs_to :author | |
belongs_to :post | |
end | |
class Post < ActiveRecord::Base | |
has_many :authorships | |
has_many :authors, through: :authorships | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
author_1 = Author.create!(id: 1) | |
author_2 = Author.create!(id: 2) | |
post_1 = Post.create!(id: 1, authors: [author_1]) | |
post_2 = Post.create!(id: 2, authors: [author_1]) | |
post_3 = Post.create!(id: 3, authors: [author_2]) | |
post_4 = Post.create!(id: 4, authors: [author_2]) | |
Comment.create!(id: 1, post: post_1, created_at: 4.days.ago) | |
Comment.create!(id: 2, post: post_1, created_at: 2.days.ago) | |
Comment.create!(id: 3, post: post_2, created_at: 4.days.ago) | |
Comment.create!(id: 4, post: post_2, created_at: 2.days.ago) | |
Comment.create!(id: 5, post: post_3, created_at: 4.days.ago) | |
Comment.create!(id: 6, post: post_3, created_at: 2.days.ago) | |
Comment.create!(id: 7, post: post_4, created_at: 4.days.ago) | |
Comment.create!(id: 8, post: post_4, created_at: 2.days.ago) | |
relation = Post | |
.joins(:authors, :comments) # <--- Issue #608 was caused by missing `:comments` join. | |
.ransack( | |
# Swap the following two lines: | |
comments_created_at_gteq: 3.days.ago, | |
authors_id_eq: author_1.id | |
) | |
.result | |
begin | |
puts "\r\n#{relation.to_sql}\r\n\r\n" | |
relation.to_a | |
puts "👍 All is well." | |
rescue | |
puts "💣 Invalid SQL!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment