Skip to content

Instantly share code, notes, and snippets.

@Edouard-chin
Created December 11, 2017 19:27
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 Edouard-chin/f56d464a0adcb76962afc1a9134a1536 to your computer and use it in GitHub Desktop.
Save Edouard-chin/f56d464a0adcb76962afc1a9134a1536 to your computer and use it in GitHub Desktop.
Columns not quoted when using a `from` clause
# frozen_string_literal: true
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'activerecord', github: 'rails/rails', ref: 'c03a39998156f4496127540cc4bc276e67fd5901'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
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.integer :name
t.string :where
t.timestamps
end
end
class Post < ActiveRecord::Base
self.ignored_columns = %w(name)
scope :using_index, -> { from('`posts`') } # from in this case is useless but is useful when trying to use a specific index
end
class BugTest < Minitest::Test
def test_association_stuff
Post.create!
Post.using_index.first # Will fail SELECT id, where, created_at, updated_at FROM `posts`
# "where" isn't quoted, issue would be the same with any columns having a name that is as database specific keyword
# the most common on would be the `key`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment