Skip to content

Instantly share code, notes, and snippets.

View benjaminwood's full-sized avatar
👨‍💻
Workin'

Benjamin Wood benjaminwood

👨‍💻
Workin'
View GitHub Profile
@benjaminwood
benjaminwood / active_record_objects_autosave.md
Created January 4, 2018 20:18 — forked from demisx/active_record_objects_autosave.md
When Active Record Child Objects are Autosaved in Rails

belongs_to:

  1. Assigning an object to a belongs_to association does not automatically save the object. It does not save the associated object either.

has_one:

  1. When you assign an object to a has_one association, that object is automatically saved (in order to update its foreign key).
  2. In addition, any object being replaced is also automatically saved, because its foreign key will change too
  3. If either of these saves fails due to validation errors, then the assignment statement returns false and the assignment itself is cancelled.
  4. If the parent object (the one declaring the has_one association) is unsaved (that is, new_record? returns true) then the child objects are not saved. They will automatically when the parent object is saved.
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/protocol.rb:186:in `rbuf_fill'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/protocol.rb:154:in `readuntil'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/protocol.rb:164:in `readline'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/http/response.rb:40:in `read_status_line'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/http/response.rb:29:in `read_new'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/http.rb:1446:in `block in transport_request'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/http.rb:1443:in `catch'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/http.rb:1443:in `transport_request'",
"/home/deploy/.rbenv/versions/2.4.3/lib/ruby/2.4.0/net/http.rb:1416:in `request'",
"/gems/gems/webmock-2.3.1/lib/webmock/http_lib_adapters/net_http.rb:97:in `block in request'",
@benjaminwood
benjaminwood / rspec has_one build bug
Created February 10, 2019 22:34
Test case reproducing an issue that causes a record to be inserted without a require foreign key (foo_id)
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", "5.2.2"
gem "sqlite3", '~> 1.3.13'
gem "factory_bot", "4.11.1"
@benjaminwood
benjaminwood / command.sh
Created May 10, 2020 18:35
My On Zoom weekend project
#!/bin/bash
# Replace with your D1 IP (see it in serial monitor when the devboard starts up)
url=http://192.168.179.146/LED
while :
do
if xwininfo -tree -root | grep "Zoom Meeting" &> /dev/null
then
curl -d color=red $url
@benjaminwood
benjaminwood / install-stripe.sh
Created June 9, 2021 21:16
Install latest stripe cli from github releases
# This assumes jq is installed already.
# The stripe binary will be installed at /bin/stripe
curl --silent "https://api.github.com/repos/stripe/stripe-cli/releases/latest" \
| jq -r '.assets[] | select(.name | endswith("linux_x86_64.tar.gz")).browser_download_url' \
| wget -qi - -O - \
| sudo tar -xzvf - -C /bin stripe > /dev/null