Skip to content

Instantly share code, notes, and snippets.

@robmathews
Last active November 25, 2016 14:36
Show Gist options
  • Save robmathews/b00bd2319ab4fd1768ab57224b6ed895 to your computer and use it in GitHub Desktop.
Save robmathews/b00bd2319ab4fd1768ab57224b6ed895 to your computer and use it in GitHub Desktop.
Don't gimp "OR", let that be the users problem
Resolving dependencies...
Using rake 11.2.2 (was 11.1.2)
Using concurrent-ruby 1.0.2
Using i18n 0.7.0
Using minitest 5.9.0 (was 5.3.3)
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.1.0 (was 2.0.0)
Using pkg-config 1.1.7
Using json 1.8.3
Using nio4r 1.2.1
Using websocket-extensions 0.1.2
Using mime-types-data 3.2016.0521
Using arel 7.0.0
Using bundler 1.12.3
Using method_source 0.8.2
Using thor 0.19.1
Using sqlite3 1.3.11
Using tzinfo 1.2.2
Using nokogiri 1.6.8 (was 1.6.7.2)
Using rack 2.0.0.rc1
Using websocket-driver 0.6.4 (was 0.6.3)
Using mime-types 3.1
Using activesupport 5.0.0.rc2 from source at `.`
Using loofah 2.0.3
Using rack-test 0.6.3
Using sprockets 3.6.2
Using mail 2.6.4
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.6
Using activemodel 5.0.0.rc2 from source at `.`
Using rails-html-sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using activejob 5.0.0.rc2 from source at `.`
Using activerecord 5.0.0.rc2 from source at `.`
Using actionview 5.0.0.rc2 from source at `.`
Using actionpack 5.0.0.rc2 from source at `.`
Using actioncable 5.0.0.rc2 from source at `.`
Using actionmailer 5.0.0.rc2 from source at `.`
Using railties 5.0.0.rc2 from source at `.`
Using sprockets-rails 3.0.4
Using rails 5.0.0.rc2 from source at `.`
-- create_table(:posts, {:force=>true})
D, [2016-06-23T14:35:38.899532 #36751] DEBUG -- :  (0.2ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar)
-> 0.0220s
-- create_table(:comments, {:force=>true})
D, [2016-06-23T14:35:38.900058 #36751] DEBUG -- :  (0.1ms) CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer, "spam" boolean)
-> 0.0005s
D, [2016-06-23T14:35:38.903402 #36751] DEBUG -- :  (0.1ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
D, [2016-06-23T14:35:38.910864 #36751] DEBUG -- : ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", :environment], ["LIMIT", 1]]
D, [2016-06-23T14:35:38.914502 #36751] DEBUG -- :  (0.1ms) begin transaction
D, [2016-06-23T14:35:38.915399 #36751] DEBUG -- : SQL (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", 2016-06-23 18:35:38 UTC], ["updated_at", 2016-06-23 18:35:38 UTC]]
D, [2016-06-23T14:35:38.915568 #36751] DEBUG -- :  (0.0ms) commit transaction
Run options: --seed 58868
# Running:
D, [2016-06-23T14:35:38.934429 #36751] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T14:35:38.935132 #36751] DEBUG -- : SQL (0.1ms) INSERT INTO "posts" ("title") VALUES (?) [["title", "Cat Videos"]]
D, [2016-06-23T14:35:38.935378 #36751] DEBUG -- :  (0.0ms) commit transaction
D, [2016-06-23T14:35:38.945755 #36751] DEBUG -- :  (0.1ms) begin transaction
D, [2016-06-23T14:35:38.946522 #36751] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" ("spam") VALUES (?) [["spam", false]]
D, [2016-06-23T14:35:38.946777 #36751] DEBUG -- :  (0.0ms) commit transaction
D, [2016-06-23T14:35:38.947552 #36751] DEBUG -- :  (0.1ms) begin transaction
D, [2016-06-23T14:35:38.950216 #36751] DEBUG -- : SQL (0.1ms) UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = ? [["post_id", 1], ["id", 1]]
D, [2016-06-23T14:35:38.950414 #36751] DEBUG -- :  (0.1ms) commit transaction
D, [2016-06-23T14:35:38.950609 #36751] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T14:35:38.950980 #36751] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" ("spam") VALUES (?) [["spam", true]]
D, [2016-06-23T14:35:38.951105 #36751] DEBUG -- :  (0.0ms) commit transaction
D, [2016-06-23T14:35:38.951217 #36751] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T14:35:38.951685 #36751] DEBUG -- : SQL (0.0ms) UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = ? [["post_id", 1], ["id", 2]]
D, [2016-06-23T14:35:38.951831 #36751] DEBUG -- :  (0.0ms) commit transaction
SELECT "posts"."id" AS t0_r0, "posts"."title" AS t0_r1, "comments"."id" AS t1_r0, "comments"."post_id" AS t1_r1, "comments"."spam" AS t1_r2 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE ("comments"."spam" = 't' OR "posts"."title" = 'Cat Videos')
D, [2016-06-23T14:35:38.957846 #36751] DEBUG -- : SQL (0.2ms) SELECT "posts"."id" AS t0_r0, "posts"."title" AS t0_r1, "comments"."id" AS t1_r0, "comments"."post_id" AS t1_r1, "comments"."spam" AS t1_r2 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE ("comments"."spam" = ? OR "posts"."title" = ?) [["spam", true], ["title", "Cat Videos"]]
F
Finished in 0.036132s, 27.6762 runs/s, 27.6762 assertions/s.
1) Failure:
BugTest#test_association_stuff [example.rb:51]:
--- expected
+++ actual
@@ -1 +1 @@
-2
+#<ActiveRecord::Relation [#<Post id: 1, title: "Cat Videos">]>
1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
Resolving dependencies...
Using rake 11.2.2 (was 11.1.2)
Using concurrent-ruby 1.0.2
Using i18n 0.7.0
Using minitest 5.9.0 (was 5.3.3)
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.1.0 (was 2.0.0)
Using pkg-config 1.1.7
Using json 1.8.3
Using nio4r 1.2.1
Using websocket-extensions 0.1.2
Using mime-types-data 3.2016.0521
Using arel 7.0.0
Using bundler 1.12.3
Using method_source 0.8.2
Using thor 0.19.1
Using sqlite3 1.3.11
Using tzinfo 1.2.2
Using nokogiri 1.6.8 (was 1.6.7.2)
Using rack 2.0.0.rc1
Using websocket-driver 0.6.4 (was 0.6.3)
Using mime-types 3.1
Using activesupport 5.0.0.rc2 from source at `.`
Using loofah 2.0.3
Using rack-test 0.6.3
Using sprockets 3.6.2
Using mail 2.6.4
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.6
Using activemodel 5.0.0.rc2 from source at `.`
Using rails-html-sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using activejob 5.0.0.rc2 from source at `.`
Using activerecord 5.0.0.rc2 from source at `.`
Using actionview 5.0.0.rc2 from source at `.`
Using actionpack 5.0.0.rc2 from source at `.`
Using actioncable 5.0.0.rc2 from source at `.`
Using actionmailer 5.0.0.rc2 from source at `.`
Using railties 5.0.0.rc2 from source at `.`
Using sprockets-rails 3.0.4
Using rails 5.0.0.rc2 from source at `.`
-- create_table(:posts, {:force=>true})
D, [2016-06-23T13:58:04.655760 #35739] DEBUG -- :  (0.2ms) CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar)
-> 0.0196s
-- create_table(:comments, {:force=>true})
D, [2016-06-23T13:58:04.656403 #35739] DEBUG -- :  (0.1ms) CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer, "spam" boolean)
-> 0.0005s
D, [2016-06-23T13:58:04.659977 #35739] DEBUG -- :  (0.1ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
D, [2016-06-23T13:58:04.667879 #35739] DEBUG -- : ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", :environment], ["LIMIT", 1]]
D, [2016-06-23T13:58:04.671285 #35739] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T13:58:04.672021 #35739] DEBUG -- : SQL (0.1ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", 2016-06-23 17:58:04 UTC], ["updated_at", 2016-06-23 17:58:04 UTC]]
D, [2016-06-23T13:58:04.672153 #35739] DEBUG -- :  (0.0ms) commit transaction
Run options: --seed 26885
# Running:
D, [2016-06-23T13:58:04.690211 #35739] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T13:58:04.690923 #35739] DEBUG -- : SQL (0.1ms) INSERT INTO "posts" ("title") VALUES (?) [["title", "Cat Videos"]]
D, [2016-06-23T13:58:04.691104 #35739] DEBUG -- :  (0.0ms) commit transaction
D, [2016-06-23T13:58:04.699739 #35739] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T13:58:04.700274 #35739] DEBUG -- : SQL (0.1ms) INSERT INTO "comments" ("spam") VALUES (?) [["spam", false]]
D, [2016-06-23T13:58:04.700423 #35739] DEBUG -- :  (0.0ms) commit transaction
D, [2016-06-23T13:58:04.700538 #35739] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T13:58:04.703048 #35739] DEBUG -- : SQL (0.1ms) UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = ? [["post_id", 1], ["id", 1]]
D, [2016-06-23T13:58:04.703248 #35739] DEBUG -- :  (0.0ms) commit transaction
D, [2016-06-23T13:58:04.703505 #35739] DEBUG -- :  (0.1ms) begin transaction
D, [2016-06-23T13:58:04.703932 #35739] DEBUG -- : SQL (0.0ms) INSERT INTO "comments" ("spam") VALUES (?) [["spam", true]]
D, [2016-06-23T13:58:04.704063 #35739] DEBUG -- :  (0.0ms) commit transaction
D, [2016-06-23T13:58:04.704195 #35739] DEBUG -- :  (0.0ms) begin transaction
D, [2016-06-23T13:58:04.704742 #35739] DEBUG -- : SQL (0.1ms) UPDATE "comments" SET "post_id" = ? WHERE "comments"."id" = ? [["post_id", 1], ["id", 2]]
D, [2016-06-23T13:58:04.704873 #35739] DEBUG -- :  (0.0ms) commit transaction
E
Finished in 0.018663s, 53.5809 runs/s, 0.0000 assertions/s.
1) Error:
BugTest#test_association_stuff:
ArgumentError: Relation passed to #or must be structurally compatible. Incompatible values: [:references]
/Users/rob/Projects/rails5/activerecord/lib/active_record/relation/query_methods.rb:675:in `or!'
/Users/rob/Projects/rails5/activerecord/lib/active_record/relation/query_methods.rb:668:in `or'
example.rb:50:in `test_association_stuff'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', :path => "." #github: 'rails/rails'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
end
create_table :comments, force: true do |t|
t.integer :post_id
t.boolean :spam
end
end
class Post < ActiveRecord::Base
has_many :comments
scope :spammy, lambda {eager_load(:comments).where(:comments=>{:spam=>true})}
scope :cat_videos, lambda {where(:title=>'Cat Videos')}
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!(:title=>'Cat Videos')
post.comments << Comment.create!(:spam=>false)
post.comments << Comment.create!(:spam=>true)
# Spammy posts and cat videos! Yum!
puts Post.spammy.or(Post.cat_videos).to_sql
assert_equal 2, Post.spammy.or(Post.cat_videos)
end
end
@hubertlepicki
Copy link

@robmathews did you sort it out? I have the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment