Skip to content

Instantly share code, notes, and snippets.

@maxcal
maxcal / log.sql
Last active December 29, 2022 14:55
User Exists? (1.3ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = $1 LIMIT $2 [["username", "michael"], ["LIMIT", 1]]
User Create (0.5ms) INSERT INTO "users" ("username", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["username", "michael"], ["created_at", "2022-12-29 14:51:02.384023"], ["updated_at", "2022-12-29 14:51:02.384023"]]
User Exists? (0.4ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = $1 LIMIT $2 [["username", "lebron"], ["LIMIT", 1]]
User Create (0.2ms) INSERT INTO "users" ("username", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["username", "lebron"], ["created_at", "2022-12-29 14:51:02.386506"], ["updated_at", "2022-12-29 14:51:02.386506"]]
User Exists? (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = $1 LIMIT $2 [["username", "michael"], ["LIMIT", 1]]
TRANSACTION (0.3ms) ROLLBACK
@maxcal
maxcal / test.rb
Created December 3, 2022 11:56
SO 74666328
irb(main):032:0> value={:next_page_token=>nil, :group_definitions=>nil, :results_by_time=>[{:time_period=>{:start=>"2022-10-01", :end=>"2022-11-01"}, :total=>{"BlendedCost"=>{:amount=>"49.1803785401", :unit=>"USD"}}, :groups=>[], :estimated=>false}, {:time_period=>{:start=>"2022-11-01", :end=>"2022-12-01"}, :total=>{"BlendedCost"=>{:amount=>"79.0698396954", :unit=>"USD"}}, :groups=>[], :estimated=>false}, {:time_period=>{:start=>"2022-12-01", :end=>"2022-12-03"}, :total=>{"BlendedCost"=>{:amount=>"2.6918272089", :unit=>"USD"}}, :groups=>[], :estimated=>true}], :dimension_value_attributes=>[]}
=>
{:next_page_token=>nil,
...
irb(main):033:0* sum = value[:results_by_time]&.sum do |hash|
irb(main):034:0* hash.dig(:total, 'BlendedCost', :amount)&.to_f || 0
irb(main):035:-> end
=> 130.9420454444
@maxcal
maxcal / test.rb
Created April 10, 2022 22:37
Class attributes bug?
require 'active_support/all'
require 'minitest/autorun'
class A
class_attribute :x, default: {}
end
class B < A
end
@maxcal
maxcal / benchmark.rb
Last active May 17, 2021 21:12
Arel VS gsub!
require 'benchmark'
def insert_list(table_name:, columns:, values:)
Arel::InsertManager.new.tap do |manager|
table = Arel::Table.new(table_name)
manager.into(table)
columns.each { |name| manager.columns << table[name] }
manager.values = manager.create_values_list(values)
end
end
irb(main):007:0> cats = Cat.all
irb(main):008:0> cats.reload
Cat Load (0.4ms) SELECT "cats".* FROM "cats"
=> #<ActiveRecord::Relation [#<Cat id: 1, name: "Proffessor Fluff", picture: nil, created_at: "2021-01-04 19:36:21", updated_at: "2021-01-04 19:36:21">, #<Cat id: 2, name: "Cindy Clawford", picture: nil, created_at: "2021-01-04 19:37:00", updated_at: "2021-01-04 19:37:00">, #<Cat id: 3, name: "Meowise", picture: nil, created_at: "2021-01-04 19:37:30", updated_at: "2021-01-04 19:37:30">]>
irb(main):009:0> Cat.last.update(name: 'Mr Meowise')
Cat Load (0.4ms) SELECT "cats".* FROM "cats" ORDER BY "cats"."id" DESC LIMIT ? [["LIMIT", 1]]
(0.1ms) begin transaction
Cat Update (5.1ms) UPDATE "cats" SET "name" = ?, "updated_at" = ? WHERE "cats"."id" = ? [["name", "Mr Meowise"], ["updated_at", "2021-01-04 19:38:34.919564"], ["id", 3]]
(5.3ms) commit transaction
=> true
@maxcal
maxcal / migration.rb
Created September 29, 2020 00:35
Adding a foreign key with the to_table option.
class AddAuthorToComment < ActiveRecord::Migration[6.0]
def change
add_reference :comments, :author, null: false, foreign_key: { to_table: :users }
end
end
class AddFeatureGroupIdToFeatures < ActiveRecord::Migration[6.0]
def change
add_refererence :features, :feature_group, foreign_key: true
end
end
@maxcal
maxcal / benchmark.rb
Created May 5, 2020 20:56
The great TR vs GSUB battle
require 'benchmark'
def vowel_cipher(string)
string.tr "aAeEiIoOuU", "eEiIoOuUaA"
end
NEXT_VOWEL = {"a"=>"e", "A"=>"E", "e"=>"i", "E"=>"I", "i"=>"o",
"I"=>"O", "o"=>"u", "O"=>"U", "u"=>"a", "U"=>"A"}
def vowel_cipher_gsub(str)
@maxcal
maxcal / railsc_output
Created April 22, 2020 10:16
Importing countries from restcountries
Running via Spring preloader in process 13691
Loading development environment (Rails 6.0.2.1)
irb(main):001:0> CountryImporterJob.perform_now('africa')
Performing CountryImporterJob (Job ID: 10d7fa89-fb75-4e98-b007-78e3544af766) from Async(default) enqueued at with arguments: "africa"
(0.2ms) BEGIN
Country Create (0.9ms) INSERT INTO "countries" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["name", "Algeria"], ["created_at", "2020-04-22 10:13:32.879869"], ["updated_at", "2020-04-22 10:13:32.879869"]]
(1.7ms) COMMIT
(0.2ms) BEGIN
Country Create (0.5ms) INSERT INTO "countries" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["name", "Angola"], ["created_at", "2020-04-22 10:13:32.897737"], ["updated_at", "2020-04-22 10:13:32.897737"]]
(1.1ms) COMMIT
@maxcal
maxcal / associations.rb
Last active February 11, 2020 16:31
Assocation Decoration in Rails
module MyApp
module Assocations
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def decorate_association(**options, &block)
yield AssocationDecorator.new(self, options)