Skip to content

Instantly share code, notes, and snippets.

View kamipo's full-sized avatar

Ryuta Kamizono kamipo

View GitHub Profile
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
@kamipo
kamipo / application_record.rb
Created March 14, 2019 22:20
Optimizer Hints
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.optimizer_hints(hints)
hints.present? ? extending(OptimizerHints.new(hints.to_s)) : all
end
class OptimizerHints < Module
def initialize(hints)
define_method(:build_arel) do |*args|
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class ApplicationRecord < ActiveRecord::Base
def foo(x)
a = x.flat_map(&:bar)
(a + [1]).flatten
end
def foo2(x)
a = x.flat_map(&:bar)
a + [1]
end
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
# frozen_string_literal: true
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
# frozen_string_literal: true
gem "bundler", "< 1.16"
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
root@localhost [test] > create table t (a int, b int, c varchar(255), index (a, b));
Query OK, 0 rows affected (0.03 sec)
root@localhost [test] > insert into t values
-> (200, 200, 'aaa'),
-> (199, 199, 'aaa'),
-> (198, 198, 'aaa'),
-> (197, 197, 'aaa'),
-> (196, 196, 'aaa'),
-> (195, 195, 'aaa'),
# frozen_string_literal: true
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
relation = Job.order("id DESC").limit(20)
relation = relation.where(user: current_user, status: "success")
relation = relation.joins(:recent_job)
# this query succeed if excepted the order in the relation.
#
# SELECT 1 AS one FROM `jobs` INNER JOIN `recent_jobs` ON `recent_jobs`.`job_id` = `jobs`.`id` WHERE `jobs`.`status` = 'success' AND `jobs`.`user_id` = 1 LIMIT 1
if relation.exists?
# But originally this query is correct to be an error.
#