Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created November 18, 2013 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonleighton/7537008 to your computer and use it in GitHub Desktop.
Save jonleighton/7537008 to your computer and use it in GitHub Desktop.
# Rails 4.0
-- create_table(:posts)
-> 0.0217s
-- 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" WHERE "comments"."post_id" = ? ORDER BY comments.id desc
SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? ORDER BY comments.id desc
# Rails edge
-- create_table(:posts)
-> 0.0056s
-- create_table(:comments)
-> 0.0005s
4.1.0.beta
SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? ORDER BY comments.id desc
SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ?
SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ?
gem "rails", "~> 4.0.0"
require "active_record"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
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.except(:order).to_sql
puts post.comments.unscope(:order).to_sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment