Skip to content

Instantly share code, notes, and snippets.

@r7kamura
Last active November 3, 2020 07:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r7kamura/6996d1423bf9aac3312ab078acf50a04 to your computer and use it in GitHub Desktop.
Save r7kamura/6996d1423bf9aac3312ab078acf50a04 to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activerecord', '6.0.0'
gem 'sqlite3'
end
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users, force: true
create_table :articles, force: true do |t|
t.integer :user_id, null: false
end
create_table :chapters, force: true do |t|
t.integer :article_id, null: false
end
end
class User < ActiveRecord::Base
has_many :articles, dependent: :destroy
end
class Article < ActiveRecord::Base
belongs_to :user
has_many :chapters, dependent: :destroy
end
class Chapter < ActiveRecord::Base
belongs_to :article
end
user = User.create!
article = user.articles.create!
article.chapters.create!
user.destroy!
@r7kamura
Copy link
Author

Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using concurrent-ruby 1.1.5
Using i18n 1.7.0
Using minitest 5.13.0
Using thread_safe 0.3.6
Using tzinfo 1.2.6
Using zeitwerk 2.2.2
Using activesupport 6.0.0
Using activemodel 6.0.0
Using activerecord 6.0.0
Using bundler 2.1.2
Fetching sqlite3 1.4.2
Installing sqlite3 1.4.2 with native extensions
-- create_table(:users, {:force=>true})
D, [2019-12-29T02:27:22.129228 #66146] DEBUG -- :    (1.5ms)  SELECT sqlite_version(*)
D, [2019-12-29T02:27:22.129695 #66146] DEBUG -- :    (0.1ms)  DROP TABLE IF EXISTS "users"
D, [2019-12-29T02:27:22.130557 #66146] DEBUG -- :    (0.6ms)  CREATE TABLE "users" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
   -> 0.0150s
-- create_table(:articles, {:force=>true})
D, [2019-12-29T02:27:22.130925 #66146] DEBUG -- :    (0.1ms)  DROP TABLE IF EXISTS "articles"
D, [2019-12-29T02:27:22.131234 #66146] DEBUG -- :    (0.1ms)  CREATE TABLE "articles" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer NOT NULL)
   -> 0.0006s
-- create_table(:chapters, {:force=>true})
D, [2019-12-29T02:27:22.131542 #66146] DEBUG -- :    (0.1ms)  DROP TABLE IF EXISTS "chapters"
D, [2019-12-29T02:27:22.131857 #66146] DEBUG -- :    (0.1ms)  CREATE TABLE "chapters" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "article_id" integer NOT NULL)
   -> 0.0006s
D, [2019-12-29T02:27:22.198106 #66146] DEBUG -- :    (0.2ms)  CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL)
D, [2019-12-29T02:27:22.212853 #66146] DEBUG -- :   ActiveRecord::InternalMetadata Load (0.1ms)  SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ?  [["key", "environment"], ["LIMIT", 1]]
D, [2019-12-29T02:27:22.217788 #66146] DEBUG -- :    (0.1ms)  begin transaction
D, [2019-12-29T02:27:22.218258 #66146] DEBUG -- :   ActiveRecord::InternalMetadata Create (0.2ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["key", "environment"], ["value", "default_env"], ["created_at", "2019-12-29 02:27:22.217241"], ["updated_at", "2019-12-29 02:27:22.217241"]]
D, [2019-12-29T02:27:22.218490 #66146] DEBUG -- :    (0.0ms)  commit transaction
D, [2019-12-29T02:27:22.227795 #66146] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-12-29T02:27:22.227929 #66146] DEBUG -- :   User Create (0.1ms)  INSERT INTO "users" DEFAULT VALUES
D, [2019-12-29T02:27:22.228112 #66146] DEBUG -- :    (0.0ms)  commit transaction
D, [2019-12-29T02:27:22.241930 #66146] DEBUG -- :    (0.1ms)  begin transaction
D, [2019-12-29T02:27:22.242185 #66146] DEBUG -- :   Article Create (0.1ms)  INSERT INTO "articles" ("user_id") VALUES (?)  [["user_id", 1]]
D, [2019-12-29T02:27:22.242402 #66146] DEBUG -- :    (0.0ms)  commit transaction
D, [2019-12-29T02:27:22.245041 #66146] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-12-29T02:27:22.245245 #66146] DEBUG -- :   Chapter Create (0.1ms)  INSERT INTO "chapters" ("article_id") VALUES (?)  [["article_id", 1]]
D, [2019-12-29T02:27:22.245422 #66146] DEBUG -- :    (0.1ms)  commit transaction
D, [2019-12-29T02:27:22.246001 #66146] DEBUG -- :    (0.0ms)  begin transaction
D, [2019-12-29T02:27:22.246196 #66146] DEBUG -- :   Article Load (0.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."user_id" = ?  [["user_id", 1]]
D, [2019-12-29T02:27:22.246874 #66146] DEBUG -- :   Chapter Load (0.1ms)  SELECT "chapters".* FROM "chapters" WHERE "chapters"."article_id" = ?  [["article_id", 1]]
D, [2019-12-29T02:27:22.247313 #66146] DEBUG -- :   Chapter Destroy (0.1ms)  DELETE FROM "chapters" WHERE "chapters"."id" = ?  [["id", 1]]
D, [2019-12-29T02:27:22.247594 #66146] DEBUG -- :   Article Destroy (0.1ms)  DELETE FROM "articles" WHERE "articles"."id" = ?  [["id", 1]]
D, [2019-12-29T02:27:22.247859 #66146] DEBUG -- :   User Destroy (0.1ms)  DELETE FROM "users" WHERE "users"."id" = ?  [["id", 1]]
D, [2019-12-29T02:27:22.247986 #66146] DEBUG -- :    (0.0ms)  commit transaction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment