Skip to content

Instantly share code, notes, and snippets.

class ApplicationRecord < ActiveRecord::Base
...
after_create_commit do
ActiveRecord::Base.connection.execute("CLUSTER #{self.class.table_name}")
end
end
-- DO NOT RUN IN PRODUCTION
-- Supported on PSQL 9.6+.
-- Note: If all your tables aren’t located in the public schema
-- be sure to modify the specified schemaname
do $$
declare
selectrow record;
begin
for selectrow in
select
@abaldwin88
abaldwin88 / first_or_create_callback.rb
Last active March 15, 2019 16:33
Scoping within first_or_create callback
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
@posts = Post.all
@posts = @posts.where(published: true) unless current_admin
@posts = @posts.where(author: selected_author) unless any_author?
@posts = @posts.search_by_title(search_param).default_order
@posts = Post.search_by_title(search_param).default_order
@posts = @posts.select do |post|
(post.author == selected_author || any_author?) && (post.published? || current_admin)
end
NUMBER_OF_TRIALS = 100
NUM_OF_CHARS = 43
CHAR_LIST = ('a'..'z').to_a
SEARCH_TERMS = CHAR_LIST.map do |letter|
letter + '?' * (NUM_OF_CHARS - 1)
end
results = Hash.new { |h, k| h[k] = [] }
NUMBER_OF_TRIALS.times do |i|
NUMBER_OF_TRIALS = 100
DIGIT_LENGTH_RANGE = (10..70)
SEARCH_TERMS = DIGIT_LENGTH_RANGE.map do |i|
'?' * i
end
results = Hash.new { |h, k| h[k] = [] }
NUMBER_OF_TRIALS.times do |i|
SEARCH_TERMS.each do |term|
NUMBER_OF_TRIALS = 100
SEARCH_TERMS = ['scott', 'blake']
results = Hash.new { |h, k| h[k] = [] }
NUMBER_OF_TRIALS.times do |i|
SEARCH_TERMS.each do |term|
results[term] << benchmark_get(term)
end
end
require 'benchmark'
def benchmark_get(search_term)
bm = Benchmark.realtime do
get(search_term)
end
bm * 1000
end
require 'httparty'
URL = 'http://localhost:3000/posts?search='
def get(search_term)
HTTParty.get("#{URL}#{search_term}")
end