Skip to content

Instantly share code, notes, and snippets.

View tom-lord's full-sized avatar
🔥
🚒

Tom Lord tom-lord

🔥
🚒
View GitHub Profile
class AddPartialIndexOnDealershipIdAndExpiredAtToLeaseDeals < ActiveRecord::Migration[5.2]
def change
add_index :lease_deals, %i[dealership_id expired_at], where: 'expired_at IS NULL', algorithm: :concurrently
end
end
Bitmap Heap Scan on lease_deals (cost=448.02..903.13 rows=5903 width=205)
Recheck Cond: ((dealership_id = 1234) AND (expired_at IS NULL))
Filter: ((updated_at < '2018-10-09 09:34:11.818701'::timestamp without time zone))
-> Bitmap Index Scan on index_lease_deals_on_dealership_id_and_expired_at (cost=0.00..411.04 rows=6724 width=0)
Index Cond: (dealership_id = 1234)
Limit (cost=0.11..4232.55 rows=1000 width=4)
-> Index Scan using lease_deals_pkey on lease_deals (cost=0.11..3130311.24 rows=739600 width=4)
Filter: ((expired_at IS NULL) AND (updated_at < '2018-10-09 09:26:33.507679'::timestamp without time zone) AND (dealership_id = 5678))
# Snippets taken from:
# https://github.com/rails/rails/blob/fc5dd0b85189811062c85520fd70de8389b55aeb/activerecord/lib/active_record/relation/batches.rb#L201
def in_batches(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil)
relation = self
# ...
batch_limit = of
# ...
relation = relation.reorder(batch_order).limit(batch_limit)
relation = apply_limits(relation, start, finish)
class LeaseDeal < ApplicationRecord
def self.expire_all(time: Time.current, batch_size: 1000)
ids = pluck(:id)
ids.each_slice(batch_size) do |ids_slice|
unscoped.where(id: ids_slice).update_all(expired_at: time)
end
end
end
class AddOrderedDealershipIdExpiredAtIndexToLeaseDeals < ActiveRecord::Migration[5.2]
def change
remove_index :lease_deals, name: :index_lease_deals_on_dealership_id_and_expired_at, algorithm: :concurrently
add_index :lease_deals, %i[id dealership_id expired_at], where: 'expired_at IS NULL', using: :btree, order: { id: :asc }, algorithm: :concurrently
end
end
@tom-lord
tom-lord / rails_admin.de.yml
Created December 19, 2018 16:17 — forked from soemo/rails_admin.de.yml
German translation for RailsAdmin
# German rails_admin translation
de:
admin:
home:
name: "Home"
pagination:
previous: "&laquo; Vor"
next: "Zurück &raquo;"
truncate: "…"
@tom-lord
tom-lord / euler_square.rb
Created May 14, 2020 11:24
Find all unique solutions to the 4x4 playing cards puzzle
class Array
def no_duplicates?
tally.values.all? {|v| v == 1 }
end
end
class Card
attr_reader :picture, :suit
def initialize(picture, suit)