Skip to content

Instantly share code, notes, and snippets.

@iiwo
iiwo / easy_tags_tag_ownership.rb
Created December 25, 2023 05:31
easy_tags_tag_ownership.rb
# frozen_string_literal: true
# Tag ownership example using easy_tags
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "~> 7.0"
gem "sqlite3"
@iiwo
iiwo / enums_in_rails.md
Last active June 16, 2022 12:28
enums_in_rails.md
db/model string/no enum integer/array enum integer/hash enum string/hash enum pg enum/hash enum
migration(pg) `add_column(:scraper_re
@iiwo
iiwo / active_record_objects_autosave.md
Created April 26, 2022 14:21 — 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.
@iiwo
iiwo / through_association_autosave.rb
Last active April 26, 2022 14:45
through_association_autosave.rb
# frozen_string_literal: true
# This script illustrates caveats of ActiveRecord association autosave
# when using a combination of database constrains with a through association (join model)
#
# ## USAGE
#
# ruby through_association_autosave.rb
#
# ## EDGE CASE
@iiwo
iiwo / has_one_duplicate.rb
Last active April 11, 2022 15:53
has_one_duplicate.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "6.1"
gem "sqlite3"
end
@iiwo
iiwo / sti_versions_duplicates.rb
Last active July 9, 2021 19:31
sti_versions_duplicates.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "6.0.3.7"
gem "paper_trail", "~> 12", require: false
gem "sqlite3"
end
@iiwo
iiwo / rails_5_1.rb
Created August 23, 2019 22:59
rails_5_1.rb
# 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', '~> 5.1.7'
@iiwo
iiwo / rails_5_2.rb
Created August 23, 2019 22:58
rails_5_2.rb
# 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', '~> 5.2.3'
@iiwo
iiwo / after_commit.rb
Created March 15, 2019 15:22
after_commit.rb
# 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.
SELECT COUNT(*)
FROM "users"
LEFT OUTER JOIN blacklisted_emails b_e ON LOWER(b_e.email) = LOWER(users.email)
LEFT OUTER JOIN
(SELECT "campaign_emails".*
FROM "campaign_emails"
INNER JOIN "campaign_blasts" ON "campaign_blasts"."id" = "campaign_emails"."campaign_blast_id"
INNER JOIN "email_campaigns" ON "email_campaigns"."id" = "campaign_blasts"."email_campaign_id"
WHERE (email_campaigns.id = NULL)) c_e ON c_e.user_id = users.id
WHERE (b_e.email IS NULL)