Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created November 18, 2013 23:22
Show Gist options
  • Save jonleighton/7537203 to your computer and use it in GitHub Desktop.
Save jonleighton/7537203 to your computer and use it in GitHub Desktop.
A demonstration that "unscoped" just clobbers everything in the scope chain
-- create_table(:posts)
-> 0.0189s
-- create_table(:comments)
-> 0.0005s
4.0.1
SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? ORDER BY comments.id desc
SELECT "comments".* FROM "comments"
SELECT "comments".* FROM "comments"
gem "rails", "~> 4.0.0"
require "active_record"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
# ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Schema.define do
create_table :posts
create_table :comments do |t|
t.integer :post_id
end
end
class Comment < ActiveRecord::Base
default_scope { order "comments.id desc" }
end
class Post < ActiveRecord::Base
has_many :comments
end
puts ActiveRecord::VERSION::STRING
post = Post.create
puts post.comments.to_sql
puts post.comments.unscoped.to_sql
puts Comment.order("omg").where("wtf").unscoped.to_sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment