-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter1.rb
executable file
·80 lines (64 loc) · 1.82 KB
/
chapter1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
# From https://greg.molnar.io/blog/a-single-file-rails-application/
# Necessary for single file Rails application, not sure how I want
# to handle this yet.
gemfile(true) do
source 'https://rubygems.org'
gem 'rails'
gem 'sqlite3'
gem 'colorize'
gem 'query_count'
gem 'prosopite'
gem 'pg_query'
end
require 'active_record'
require 'logger'
require 'sqlite3'
require 'colorize'
require 'query_count'
require 'prosopite'
Prosopite.prosopite_logger = true
require_relative 'setup'
# Define post model
class User < ApplicationRecord
# self.strict_loading_by_default = true
has_many :comments
end
# Define comment model
class Comment < ApplicationRecord
belongs_to :user
end
def seed(user_count:, comment_count:)
(1..user_count).each do |i|
user = User.create(first_name: "Name#{i}")
(1..comment_count).each do |j|
user.comments.create(body: "Comment #{j}")
end
end
end
banner = <<~BANNER
Strict loading is a feature that helps you to avoid N+1 queries by
raising an error when you try to load an association that was not
preloaded. This feature is useful to ensure that you are not
accidentally loading associations in a loop or in a view.
In this case, strict loading is being set on the model instead of
the association. This means that the error will be raised when you
try to load the association from any instance of the model.
BANNER
puts banner.yellow
seed(user_count: 2, comment_count: 5)
ActiveRecord::Base.logger = Logger.new($stdout)
# Page 12. Strict loading on a relation.
# user = User.first
# begin
# user.comments.to_a
# rescue ActiveRecord::StrictLoadingViolationError => e
# puts e.message.red
# end
result = Prosopite.scan do
user = User.first
user.comments.to_a
end
puts "Result: #{result}"