Skip to content

Instantly share code, notes, and snippets.

@benhamill
Created August 6, 2013 04:46
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 benhamill/6162089 to your computer and use it in GitHub Desktop.
Save benhamill/6162089 to your computer and use it in GitHub Desktop.
includes + where + select Ignores Your select_values See [this repo](https://github.com/benhamill/ar_include_test#whats-up-with-includes) for an interactive explanation, which might aid investigation.
gem 'activerecord', '4.0.0'
require 'active_record'
require 'sqlite3'
require 'logger'
require 'minitest/autorun'
# 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 :users do |t|
t.string :name
end
create_table :comments do |t|
t.string :body
t.integer :user_id
end
end
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :user
end
class BugTest < MiniTest::Unit::TestCase
def test_includes_where_select
query = User.select('random() AS ranking').order('ranking').includes(:comments)
.where(comments: { id: 1 })
assert_equal ['random() AS ranking'], query.select_values
assert_equal [], query.to_a
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment