Slide 1

Slide 1 text

An Intervention for ActiveRecord Ernie Miller / @erniemiller / http://erniemiller.org

Slide 2

Slide 2 text

Hi. https://github.com/ernie

Slide 3

Slide 3 text

we’re hiring.* * “we’re hiring” is not part of the LivingSocial brand, but totally should be

Slide 4

Slide 4 text

“You’re interesting to listen to.” ― Mom

Slide 5

Slide 5 text

WARNING THE WARNING IN THE TALK DESCRIPTION, WHICH STATED THAT THERE WOULD BE ACTIVERECORD SOURCE CODE IN THIS TALK, WAS NOT A SCARE TACTIC.

Slide 6

Slide 6 text

WARNING THE WARNING IN THE TALK DESCRIPTION, WHICH STATED THAT THERE WOULD BE ACTIVERECORD SOURCE CODE IN THIS TALK, WAS NOT A SCARE TACTIC. WE WILL BE READING ACTIVERECORD SOURCE CODE IN THIS TALK.

Slide 7

Slide 7 text

WARNING THE WARNING IN THE TALK DESCRIPTION, WHICH STATED THAT THERE WOULD BE ACTIVERECORD SOURCE CODE IN THIS TALK, WAS NOT A SCARE TACTIC. WE WILL BE READING ACTIVERECORD SOURCE CODE IN THIS TALK. A LOT OF IT.

Slide 8

Slide 8 text

WARNING THE WARNING IN THE TALK DESCRIPTION, WHICH STATED THAT THERE WOULD BE ACTIVERECORD SOURCE CODE IN THIS TALK, WAS NOT A SCARE TACTIC. WE WILL BE READING ACTIVERECORD SOURCE CODE IN THIS TALK. A LOT OF IT. THINGS MIGHT GET CRAZY.

Slide 9

Slide 9 text

WARNING THE WARNING IN THE TALK DESCRIPTION, WHICH STATED THAT THERE WOULD BE ACTIVERECORD SOURCE CODE IN THIS TALK, WAS NOT A SCARE TACTIC. WE WILL BE READING ACTIVERECORD SOURCE CODE IN THIS TALK. A LOT OF IT. THINGS MIGHT GET CRAZY. WILL

Slide 10

Slide 10 text

WARNING THE WARNING IN THE TALK DESCRIPTION, WHICH STATED THAT THERE WOULD BE ACTIVERECORD SOURCE CODE IN THIS TALK, WAS NOT A SCARE TACTIC. WE WILL BE READING ACTIVERECORD SOURCE CODE IN THIS TALK. A LOT OF IT. THERE IS STILL TIME TO ESCAPE. THINGS MIGHT GET CRAZY. WILL

Slide 11

Slide 11 text

WHAT IS ACTIVE RECORD?

Slide 12

Slide 12 text

“An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.” A PATTERN

Slide 13

Slide 13 text

“Active Record has the primary advantage of simplicity. It’s easy to build Active Records, and they are easy to understand.” A PATTERN

Slide 14

Slide 14 text

A LIBRARY class User < ActiveRecord::Base # name, email, and timestamps end

Slide 15

Slide 15 text

A LIBRARY class User < ActiveRecord::Base # name, email, and timestamps end User.ancestors.size # => 57 User.methods.size # => 559 User.instance_methods.size # => 352

Slide 16

Slide 16 text

A LIBRARY $ cloc lib/active_record ------------------------------ Language files code ------------------------------ Ruby 147 15836 YAML 1 11 ------------------------------ SUM: 148 15847 ------------------------------

Slide 17

Slide 17 text

“Active Record has the primary advantage of simplicity. It’s easy to build Active Records, and they are easy to understand.”

Slide 18

Slide 18 text

$ cloc lib/active_record ------------------------------ Language files code ------------------------------ Ruby 147 15836 YAML 1 11 ------------------------------ SUM: 148 15847 ------------------------------ User.ancestors.size # => 57 User.methods.size # => 559 User.instance_methods.size # => 352

Slide 19

Slide 19 text

simplicity

Slide 20

Slide 20 text

57 ancestors 559 class methods 352 instance methods

Slide 21

Slide 21 text

easy to understand

Slide 22

Slide 22 text

148 files 15,847 lines of code

Slide 23

Slide 23 text

ActiveRecord Active Record

Slide 24

Slide 24 text

ActiveRecord Active Record ≠

Slide 25

Slide 25 text

I ActiveRecord

Slide 26

Slide 26 text

...but...

Slide 27

Slide 27 text

BUT

Slide 28

Slide 28 text

ActiveRecord has issues

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

I HAVE A THEORY

Slide 32

Slide 32 text

NOBODY UNDERSTANDS ACTIVERECORD

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

YOU ARE NOT THE EXCEPTION

Slide 35

Slide 35 text

YOU ARE NOT THE EXCEPTION No. You’re not.

Slide 36

Slide 36 text

Library LOC ActionMailer 527 ActiveModel 1,601 ActionController 2,557 ActionView 6,317 ActionDispatch 7,313 ActiveSupport 9,150 ActiveRecord 15,847

Slide 37

Slide 37 text

“Magic is not inherently a bad word.” ― ActiveRecord README

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

MAGIC REQUIRES CONVENTIONS

Slide 44

Slide 44 text

MAGIC REQUIRES CONVENTIONS CONVENTIONS REQUIRE OPINIONS

Slide 45

Slide 45 text

MAGIC REQUIRES CONVENTIONS OPINIONS ARE NOT CREATED EQUAL CONVENTIONS REQUIRE OPINIONS

Slide 46

Slide 46 text

HOW TO CREATE A LEARNING CURVE

Slide 47

Slide 47 text

HOW TO CREATE A LEARNING CURVE 1.Find things your users understand.

Slide 48

Slide 48 text

HOW TO CREATE A LEARNING CURVE 1.Find things your users understand. 2.Ignore those things.

Slide 49

Slide 49 text

OBJECT INITIALIZATION

Slide 50

Slide 50 text

class User < ActiveRecord::Base def initialize(*) super self.name ||= '' end end user = User.new user.name # => ""

Slide 51

Slide 51 text

class User < ActiveRecord::Base def initialize(*) super self.name ||= '' end end user = User.new user.name # => "" user = User.where(name: nil).first user.name # => nil

Slide 52

Slide 52 text

class User < ActiveRecord::Base after_initialize :set_defaults def set_defaults self.name ||= '' end end user = User.new user.name # => "" user = User.where(name: nil).first user.name # => ""

Slide 53

Slide 53 text

core.rb: def initialize(attributes = nil) defaults = self.class.column_defaults.dup defaults.each { |k, v| defaults[k] = v.dup if v.duplicable? } @attributes = self.class.initialize_attributes(defaults) @columns_hash = self.class.column_types.dup init_internals init_changed_attributes ensure_proper_type populate_with_current_scope_attributes assign_attributes(attributes) if attributes yield self if block_given? run_callbacks :initialize unless _initialize_callbacks.empty? end

Slide 54

Slide 54 text

core.rb: def init_with(coder) @attributes = self.class.initialize_attributes( coder['attributes'] ) @columns_hash = self.class.column_types.merge( coder['column_types'] || {} ) init_internals @new_record = false run_callbacks :find run_callbacks :initialize self end

Slide 55

Slide 55 text

core.rb: def init_with(coder) @attributes = self.class.initialize_attributes( coder['attributes'] ) @columns_hash = self.class.column_types.merge( coder['column_types'] || {} ) init_internals @new_record = false run_callbacks :find run_callbacks :initialize self end

Slide 56

Slide 56 text

core.rb: def init_with(coder) @attributes = self.class.initialize_attributes( coder['attributes'] ) @columns_hash = self.class.column_types.merge( coder['column_types'] || {} ) init_internals @new_record = false run_callbacks :find run_callbacks :initialize self end

Slide 57

Slide 57 text

persistence.rb: def instantiate(record, column_types = {}) klass = discriminate_class_for_record(record) column_types = klass.decorate_columns(column_types) klass.allocate.init_with( 'attributes' => record, 'column_types' => column_types ) end def find_by_sql(sql, binds = []) result_set = connection.select_all( sanitize_sql(sql), "#{name} Load", binds ) column_types = result_set.column_types result_set.map { |record| instantiate(record, column_types) } end querying.rb: * * Deprecation warning omitted

Slide 58

Slide 58 text

persistence.rb: def instantiate(record, column_types = {}) klass = discriminate_class_for_record(record) column_types = klass.decorate_columns(column_types) klass.allocate.init_with( 'attributes' => record, 'column_types' => column_types ) end def find_by_sql(sql, binds = []) result_set = connection.select_all( sanitize_sql(sql), "#{name} Load", binds ) column_types = result_set.column_types result_set.map { |record| instantiate(record, column_types) } end querying.rb: * * Deprecation warning omitted

Slide 59

Slide 59 text

persistence.rb: def instantiate(record, column_types = {}) klass = discriminate_class_for_record(record) column_types = klass.decorate_columns(column_types) klass.allocate.init_with( 'attributes' => record, 'column_types' => column_types ) end def find_by_sql(sql, binds = []) result_set = connection.select_all( sanitize_sql(sql), "#{name} Load", binds ) column_types = result_set.column_types result_set.map { |record| instantiate(record, column_types) } end querying.rb: * * Deprecation warning omitted

Slide 60

Slide 60 text

TRADE-OFFS Understand them, then question them.

Slide 61

Slide 61 text

ASSOCIATIONS

Slide 62

Slide 62 text

ASSOCIATIONS Simple.

Slide 63

Slide 63 text

ASSOCIATIONS Simple.Until they aren’t.

Slide 64

Slide 64 text

POP QUIZ!

Slide 65

Slide 65 text

class User < ActiveRecord::Base has_many :posts def assign_posts(post_or_posts) posts = Array(post_or_posts) posts.each { |post| post.published = false } self.posts += posts end end user.assign_posts posts user.posts.all? { |post| post.user_id == user.id } Q:

Slide 66

Slide 66 text

A:

Slide 67

Slide 67 text

I HAVE ABSOLUTELY NO IDEA. A:

Slide 68

Slide 68 text

I HAVE ABSOLUTELY NO IDEA. A: Extra credit: “You’re not even testing the right attribute, genius.”

Slide 69

Slide 69 text

Already Persisted? Already Persisted? After Assignment After Assignment User Posts Posts saved ID match

Slide 70

Slide 70 text

class User < ActiveRecord::Base has_many :posts has_many :published_posts, -> { where published: true }, class_name: 'Post' end User.joins(:posts).where(posts: {title: 'zomg first post!'}) SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" WHERE "posts"."title" = 'zomg first post!'

Slide 71

Slide 71 text

class User < ActiveRecord::Base has_many :posts has_many :published_posts, -> { where published: true }, class_name: 'Post' end User.joins(:posts).where(posts: {title: 'zomg first post!'}) User.joins(:published_posts). where(published_posts: {title: 'zomg first post!'}) SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" WHERE "posts"."title" = 'zomg first post!'

Slide 72

Slide 72 text

class User < ActiveRecord::Base has_many :posts has_many :published_posts, -> { where published: true }, class_name: 'Post' end User.joins(:posts).where(posts: {title: 'zomg first post!'}) User.joins(:published_posts). where(published_posts: {title: 'zomg first post!'}) SELECT "users".* FROM "users" INNER JOIN "posts" ON "posts"."user_id" = "users"."id" WHERE "posts"."title" = 'zomg first post!' ActiveRecord::StatementInvalid

Slide 73

Slide 73 text

relation/query_methods.rb: def build_where(opts, other = []) case opts when String, Array [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))] when Hash attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts) attributes.values.grep(ActiveRecord::Relation) do |rel| self.bind_values += rel.bind_values end PredicateBuilder.build_from_hash(klass, attributes, table) else [opts] end end

Slide 74

Slide 74 text

relation/query_methods.rb: def build_where(opts, other = []) case opts when String, Array [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))] when Hash attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts) attributes.values.grep(ActiveRecord::Relation) do |rel| self.bind_values += rel.bind_values end PredicateBuilder.build_from_hash(klass, attributes, table) else [opts] end end

Slide 75

Slide 75 text

relation/predicate_builder.rb: def self.build_from_hash(klass, attributes, default_table) queries = [] attributes.each do |column, value| table = default_table if value.is_a?(Hash) if value.empty? queries << '1=0' else table = Arel::Table.new(column, default_table.engine) association = klass.reflect_on_association(column.to_sym) value.each do |k, v| queries.concat expand(association && association.klass, table, k, v) end end else # ... end end queries end

Slide 76

Slide 76 text

relation/predicate_builder.rb: def self.build_from_hash(klass, attributes, default_table) queries = [] attributes.each do |column, value| table = default_table if value.is_a?(Hash) if value.empty? queries << '1=0' else table = Arel::Table.new(column, default_table.engine) association = klass.reflect_on_association(column.to_sym) value.each do |k, v| queries.concat expand(association && association.klass, table, k, v) end end else # ... end end queries end

Slide 77

Slide 77 text

class User < ActiveRecord::Base has_many :contributions has_many :posts, through: :contributions end class Post < ActiveRecord::Base has_many :contributions has_many :contributors, through: :contributions, source: :user end class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :role, presence: true end

Slide 78

Slide 78 text

class User < ActiveRecord::Base has_many :contributions has_many :posts, through: :contributions end class Post < ActiveRecord::Base has_many :contributions has_many :contributors, through: :contributions, source: :user end class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :role, presence: true end

Slide 79

Slide 79 text

post = Post.new(params[:post]) post.contributors = [current_user] post.save # => false

Slide 80

Slide 80 text

post = Post.new(params[:post]) post.contributors = [current_user] post.contributions.first.role = 'author' # => NoMethodError: undefined method `role=' for nil:NilClass post = Post.new(params[:post]) post.contributors = [current_user] post.save # => false

Slide 81

Slide 81 text

post = Post.new(params[:post]) post.contributors = [current_user] post.contributions.first.role = 'author' # => NoMethodError: undefined method `role=' for nil:NilClass contribution = current_user.contributions.build(role: 'author') contribution.build_post(params[:post]) contribution.save # => true post = Post.new(params[:post]) post.contributors = [current_user] post.save # => false

Slide 82

Slide 82 text

class Post < ActiveRecord::Base has_many :contributions has_many :contributors, through: :contributions, source: :user has_many :authorships, -> { where role: 'author' }, class_name: 'Contribution' has_many :authors, through: :authorships, source: :user end post = Post.new(params[:post]) post.authors = [current_user] post.save # => true

Slide 83

Slide 83 text

module ActiveRecord module Associations class HasManyThroughAssociation < HasManyAssociation # ... def concat_records(records) ensure_not_nested records = super if owner.new_record? && records records.flatten.each do |record| build_through_record(record) end end records end # ... end end end

Slide 84

Slide 84 text

module ActiveRecord module Associations class HasManyThroughAssociation < HasManyAssociation # ... def concat_records(records) ensure_not_nested records = super if owner.new_record? && records records.flatten.each do |record| build_through_record(record) end end records end # ... end end end

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

THE TESTS

Slide 90

Slide 90 text

THE TESTS They lie.

Slide 91

Slide 91 text

class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :user_id, presence: true validates :post_id, presence: true end

Slide 92

Slide 92 text

class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :user_id, presence: true validates :post_id, presence: true end post = Post.new(params[:post]) post.contributors = [current_user] post.save # => false

Slide 93

Slide 93 text

class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :user_id, presence: true validates :post_id, presence: true end post = Post.new(params[:post]) post.contributors = [current_user] post.save # => false

Slide 94

Slide 94 text

class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :user_id, presence: true validates :post_id, presence: true end post = Post.new(params[:post]) post.contributors = [current_user] post.save # => false post.errors.messages # => {:contributions => ["is invalid"]}

Slide 95

Slide 95 text

INVERSE_OF

Slide 96

Slide 96 text

INVERSE_OF The oldest ActiveRecord feature you aren’t using

Slide 97

Slide 97 text

user = User.first contribution = user.contributions.first user.equal?(contribution.user) # => false

Slide 98

Slide 98 text

user = User.first contribution = user.contributions.first user.equal?(contribution.user) # => false

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

class Post < ActiveRecord::Base has_many :contributions, inverse_of: :post has_many :contributors, through: :contributions, source: :user end class User < ActiveRecord::Base has_many :contributions, inverse_of: :user has_many :posts, through: :contributions end class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :user, presence: true validates :post, presence: true end

Slide 102

Slide 102 text

class Post < ActiveRecord::Base has_many :contributions, inverse_of: :post has_many :contributors, through: :contributions, source: :user end class User < ActiveRecord::Base has_many :contributions, inverse_of: :user has_many :posts, through: :contributions end class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :user, presence: true validates :post, presence: true end

Slide 103

Slide 103 text

class Post < ActiveRecord::Base has_many :contributions, inverse_of: :post has_many :contributors, through: :contributions, source: :user end class User < ActiveRecord::Base has_many :contributions, inverse_of: :user has_many :posts, through: :contributions end class Contribution < ActiveRecord::Base belongs_to :user belongs_to :post validates :user, presence: true validates :post, presence: true end

Slide 104

Slide 104 text

Deep Thoughts

Slide 105

Slide 105 text

Deep Thoughts on VALIDATIONS

Slide 106

Slide 106 text

Deep Thoughts on VALIDATIONS

Slide 107

Slide 107 text

Deep Thoughts on VALIDATIONS Validations live in Ruby.

Slide 108

Slide 108 text

Deep Thoughts on VALIDATIONS

Slide 109

Slide 109 text

Deep Thoughts on VALIDATIONS They should care about Ruby objects.

Slide 110

Slide 110 text

Deep Thoughts on VALIDATIONS

Slide 111

Slide 111 text

Deep Thoughts on VALIDATIONS “association_id” is an implementation detail.

Slide 112

Slide 112 text

BUT MY ROFLSCALES!!!

Slide 113

Slide 113 text

BUT MY ROFLSCALES!!! They’ll be fine.

Slide 114

Slide 114 text

VALIDATES UNIQUENESS OF

Slide 115

Slide 115 text

VALIDATES UNIQUENESS OF Doesn’t.

Slide 116

Slide 116 text

VALIDATES UNIQUENESS OF Doesn’t. You know.

Slide 117

Slide 117 text

VALIDATES UNIQUENESS OF Doesn’t. You know. Validate uniqueness of.

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

“Using this validation method [...] does not guarantee the absence of duplicate record insertions.” ― validations/uniqueness.rb

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

“The best way to work around this problem is to add a unique index to the database table.” ― validations/uniqueness.rb

Slide 122

Slide 122 text

class User < ActiveRecord::Base validates :email, uniqueness: true end class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :email end add_index :users, :email, unique: true end end

Slide 123

Slide 123 text

class User < ActiveRecord::Base def save(*) super rescue ActiveRecord::RecordNotUnique => e errors.add(:email, :taken) false end end User.create name: 'Ernie Miller', email: 'ernie@erniemiller.org' # => true user = User.create name: 'Ernie Miller', email: 'ernie@erniemiller.org' # => false user.errors.messages # => {:email => ["has already been taken"]}

Slide 124

Slide 124 text

class Post < ActiveRecord::Base validates :title, uniqueness: { scope: :subtitle } end

Slide 125

Slide 125 text

class Post < ActiveRecord::Base def save(*) super rescue ActiveRecord::RecordNotUnique => e attr_name, *scope = e.message. match(/Key \(([^\)]+)\)/)[1].split(/,\s*/) message = errors.generate_message(attr_name, :taken) if scope.any? message << " (scope: #{scope.to_sentence})" end errors.add attr_name, message false end end post = Post.create(title: 'Hello', subtitle: 'World!') # => false post.errors.messages # => {:title => ["has already been taken (scope: subtitle)"]}

Slide 126

Slide 126 text

class Post < ActiveRecord::Base def save(*) super rescue ActiveRecord::RecordNotUnique => e attr_name, *scope = e.message. match(/Key \(([^\)]+)\)/)[1].split(/,\s*/) message = errors.generate_message(attr_name, :taken) if scope.any? message << " (scope: #{scope.to_sentence})" end errors.add attr_name, message false end end post = Post.create(title: 'Hello', subtitle: 'World!') # => false post.errors.messages # => {:title => ["has already been taken (scope: subtitle)"]}

Slide 127

Slide 127 text

RELATION#MERGE

Slide 128

Slide 128 text

RELATION#MERGE You’re using it.

Slide 129

Slide 129 text

RELATION#MERGE You’re using it. A lot.

Slide 130

Slide 130 text

def merged_wheres values[:where] ||= [] if values[:where].empty? || relation.where_values.empty? relation.where_values + values[:where] else # Remove equalities from the existing relation with a LHS which is # present in the relation being merged in. seen = Set.new values[:where].each { |w| if w.respond_to?(:operator) && w.operator == :== seen << w.left end } relation.where_values.reject { |w| w.respond_to?(:operator) && w.operator == :== && seen.include?(w.left) } + values[:where] end end relation/merger.rb:

Slide 131

Slide 131 text

# We need to be able to support merging two relations without having # to get our hooks too deeply into Active Record. That proves to be # easier said than done. I hate Relation#merge. If Squeel has a # nemesis, Relation#merge would be it. # # Whatever code you see here currently is my current best attempt at # coexisting peacefully with said nemesis. def merge(r, equalities_resolved = false) if ::ActiveRecord::Relation === r && !equalities_resolved if self.table_name != r.table_name super(r.visited) else merge_resolving_duplicate_squeel_equalities(r) end else super(r) end end squeel/adapters/active_record/relation_extensions.rb:

Slide 132

Slide 132 text

MERGES.

Slide 133

Slide 133 text

SO

Slide 134

Slide 134 text

MANY

Slide 135

Slide 135 text

FREAKING

Slide 136

Slide 136 text

MERGES.

Slide 137

Slide 137 text

DEFAULT SCOPE

Slide 138

Slide 138 text

DEFAULT SCOPE Can we please just kill it, already?

Slide 139

Slide 139 text

Relation#exec_queries def exec_queries default_scoped = with_default_scope if default_scoped.equal?(self) @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, bind_values) preload = preload_values preload += includes_values unless eager_loading? preload.each do |associations| ActiveRecord::Associations::Preloader.new(@records, associations).run end # @readonly_value is true only if set explicitly. @implicit_readonly is true if there # are JOINS and no explicit SELECT. readonly = readonly_value.nil? ? @implicit_readonly : readonly_value @records.each { |record| record.readonly! } if readonly else @records = default_scoped.to_a end @loaded = true @records end relation.rb:

Slide 140

Slide 140 text

Relation#exec_queries def exec_queries default_scoped = with_default_scope if default_scoped.equal?(self) @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, bind_values) preload = preload_values preload += includes_values unless eager_loading? preload.each do |associations| ActiveRecord::Associations::Preloader.new(@records, associations).run end # @readonly_value is true only if set explicitly. @implicit_readonly is true if there # are JOINS and no explicit SELECT. readonly = readonly_value.nil? ? @implicit_readonly : readonly_value @records.each { |record| record.readonly! } if readonly else @records = default_scoped.to_a end @loaded = true @records end relation.rb:

Slide 141

Slide 141 text

Relation#with_default_scope def with_default_scope #:nodoc: if default_scoped? && default_scope = klass.send(:build_default_scope) default_scope = default_scope.merge(self) default_scope.default_scoped = false default_scope else self end end relation.rb:

Slide 142

Slide 142 text

Relation#with_default_scope def with_default_scope #:nodoc: if default_scoped? && default_scope = klass.send(:build_default_scope) default_scope = default_scope.merge(self) default_scope.default_scoped = false default_scope else self end end relation.rb:

Slide 143

Slide 143 text

Relation#build_default_scope def build_default_scope # :nodoc: if !Base.is_a?(method(:default_scope).owner) # The user has defined their own default scope method, so call that evaluate_default_scope { default_scope } elsif default_scopes.any? evaluate_default_scope do default_scopes.inject(relation) do |default_scope, scope| if !scope.is_a?(Relation) && scope.respond_to?(:call) default_scope.merge(unscoped { scope.call }) else default_scope.merge(scope) end end end end end scoping/default.rb:

Slide 144

Slide 144 text

Relation#build_default_scope def build_default_scope # :nodoc: if !Base.is_a?(method(:default_scope).owner) # The user has defined their own default scope method, so call that evaluate_default_scope { default_scope } elsif default_scopes.any? evaluate_default_scope do default_scopes.inject(relation) do |default_scope, scope| if !scope.is_a?(Relation) && scope.respond_to?(:call) default_scope.merge(unscoped { scope.call }) else default_scope.merge(scope) end end end end end scoping/default.rb:

Slide 145

Slide 145 text

Relation#build_default_scope def build_default_scope # :nodoc: if !Base.is_a?(method(:default_scope).owner) # The user has defined their own default scope method, so call that evaluate_default_scope { default_scope } elsif default_scopes.any? evaluate_default_scope do default_scopes.inject(relation) do |default_scope, scope| if !scope.is_a?(Relation) && scope.respond_to?(:call) default_scope.merge(unscoped { scope.call }) else default_scope.merge(scope) end end end end end scoping/default.rb:

Slide 146

Slide 146 text

scope(name, scope_options = {}) “Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query.” ― scoping/named.rb

Slide 147

Slide 147 text

SCOPES

Slide 148

Slide 148 text

SCOPES Narrow your result set.

Slide 149

Slide 149 text

SCOPES Narrow your result set. Just kidding.

Slide 150

Slide 150 text

class User < ActiveRecord::Base default_scope -> { where(name: 'Jim') } scope :bobs, -> { where(name: 'Bob') } scope :toms, -> { where(name: 'Tom') } end User.where(name: 'Joe') User.bobs User.bobs.toms User.bobs.toms.where(name: 'Joe') User.where(name: 'Joe').bobs => Joe => Bob => Tom => Tom, Joe => Bob 4.0.0.beta1

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

class User < ActiveRecord::Base default_scope -> { where(name: 'Jim') } scope :bobs, -> { where(name: 'Bob') } scope :toms, -> { where(name: 'Tom') } end User.where(name: 'Joe') User.bobs User.bobs.toms User.bobs.toms.where(name: 'Joe') User.where(name: 'Joe').bobs => Joe => Bob => Bob, Tom => Bob, Tom, Joe => Joe, Bob 4.0.0.rc1

Slide 153

Slide 153 text

CALCULATIONS

Slide 154

Slide 154 text

CALCULATIONS Those are the things you do with numbers, right?

Slide 155

Slide 155 text

class Post < ActiveRecord::Base belongs_to :user def self.number_of_pages(page_size = 20) number_of_posts = self.count full_pages = number_of_posts / page_size if (number_of_posts % page_size).zero? full_pages else # We will have a page with remainders full_pages + 1 end end end

Slide 156

Slide 156 text

class Post < ActiveRecord::Base belongs_to :user def self.number_of_pages(page_size = 20) number_of_posts = self.count full_pages = number_of_posts / page_size if (number_of_posts % page_size).zero? full_pages else # We will have a page with remainders full_pages + 1 end end end NoMethodError: undefined method `/' for #

Slide 157

Slide 157 text

def perform_calculation(operation, column_name, options = {}) operation = operation.to_s.downcase distinct = self.distinct_value if operation == "count" column_name ||= (select_for_count || :all) unless arel.ast.grep(Arel::Nodes::OuterJoin).empty? distinct = true end column_name = primary_key if column_name == :all && distinct distinct = nil if column_name =~ /\s*DISTINCT\s+/i end if group_values.any? execute_grouped_calculation(operation, column_name, distinct) else execute_simple_calculation(operation, column_name, distinct) end end relation/calculations.rb: * * Deprecation warning omitted

Slide 158

Slide 158 text

def perform_calculation(operation, column_name, options = {}) operation = operation.to_s.downcase distinct = self.distinct_value if operation == "count" column_name ||= (select_for_count || :all) unless arel.ast.grep(Arel::Nodes::OuterJoin).empty? distinct = true end column_name = primary_key if column_name == :all && distinct distinct = nil if column_name =~ /\s*DISTINCT\s+/i end if group_values.any? execute_grouped_calculation(operation, column_name, distinct) else execute_simple_calculation(operation, column_name, distinct) end end relation/calculations.rb: * * Deprecation warning omitted

Slide 159

Slide 159 text

relation/calculations.rb: def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: group_attrs = group_values if group_attrs.first.respond_to?(:to_sym) association = @klass.reflect_on_association(group_attrs.first.to_sym) associated = group_attrs.size == 1 && association && association.macro == :belongs_to # only count belongs_to associations group_fields = Array(associated ? association.foreign_key : group_attrs) else group_fields = group_attrs end group_aliases = group_fields.map { |field| column_alias_for(field) } group_columns = group_aliases.zip(group_fields).map { |aliaz,field| [aliaz, field] } group = group_fields if operation == 'count' && column_name == :all aggregate_alias = 'count_all' else aggregate_alias = column_alias_for([operation, column_name].join(' ')) end select_values = [

Slide 160

Slide 160 text

relation/calculations.rb: def execute_grouped_calculation(operation, column_name, distinct) #:nodoc: group_attrs = group_values if group_attrs.first.respond_to?(:to_sym) association = @klass.reflect_on_association(group_attrs.first.to_sym) associated = group_attrs.size == 1 && association && association.macro == :belongs_to # only count belongs_to associations group_fields = Array(associated ? association.foreign_key : group_attrs) else group_fields = group_attrs end group_aliases = group_fields.map { |field| column_alias_for(field) } group_columns = group_aliases.zip(group_fields).map { |aliaz,field| [aliaz, field] } group = group_fields if operation == 'count' && column_name == :all aggregate_alias = 'count_all' else aggregate_alias = column_alias_for([operation, column_name].join(' ')) end select_values = [

Slide 161

Slide 161 text

if association key_ids = calculated_data.collect { |row| row[group_aliases.first] } key_records = association.klass.base_class.find(key_ids) key_records = Hash[key_records.map { |r| [r.id, r] }] end Hash[calculated_data.map do |row| key = group_columns.map { |aliaz, col_name| column = calculated_data.column_types.fetch(aliaz) do column_for(col_name) end type_cast_calculated_value(row[aliaz], column) } key = key.first if key.size == 1 key = key_records[key] if associated [ key, type_cast_calculated_value( row[aggregate_alias], column_for(column_name), operation ) ] end] end

Slide 162

Slide 162 text

if association key_ids = calculated_data.collect { |row| row[group_aliases.first] } key_records = association.klass.base_class.find(key_ids) key_records = Hash[key_records.map { |r| [r.id, r] }] end Hash[calculated_data.map do |row| key = group_columns.map { |aliaz, col_name| column = calculated_data.column_types.fetch(aliaz) do column_for(col_name) end type_cast_calculated_value(row[aliaz], column) } key = key.first if key.size == 1 key = key_records[key] if associated [ key, type_cast_calculated_value( row[aggregate_alias], column_for(column_name), operation ) ] end] end

Slide 163

Slide 163 text

TL;DR

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

CALLBACKS

Slide 166

Slide 166 text

CALLBACKS You’ll be sorry.

Slide 167

Slide 167 text

CALLBACKS You’ll be sorry. Don’t say I didn’t warn you.

Slide 168

Slide 168 text

“That's a total of twelve callbacks, which gives you immense power to react and prepare for each state in the Active Record life cycle.” ― active_record/callbacks.rb

Slide 169

Slide 169 text

class User < ActiveRecord::Base before_save NameSayer.new before_save :say_my_name before_save { |record| puts "My name is #{record.name}" } before_save 'puts "My name is #{self.name}"' # YES, REALLY def say_my_name puts "My name is #{self.name}" end end class NameSayer def before_save(record) puts "My name is #{record.name}" end end

Slide 170

Slide 170 text

class User < ActiveRecord::Base before_save NameSayer.new before_save :say_my_name before_save { |record| puts "My name is #{record.name}" } before_save 'puts "My name is #{self.name}"' # YES, REALLY def say_my_name puts "My name is #{self.name}" end end class NameSayer def before_save(record) puts "My name is #{record.name}" end end

Slide 171

Slide 171 text

class User < ActiveRecord::Base before_save NameSayer.new before_save :say_my_name before_save { |record| puts "My name is #{record.name}" } before_save 'puts "My name is #{self.name}"' # YES, REALLY def say_my_name puts "My name is #{self.name}" end end class NameSayer def before_save(record) puts "My name is #{record.name}" end end User.new._save_callbacks.class # => ActiveSupport::Callbacks::CallbackChain

Slide 172

Slide 172 text

active_support/callbacks.rb: class CallbackChain < Array #:nodoc:# # ... def compile method = ["value = nil", "halted = false"] callbacks = "value = !halted && (!block_given? || yield)" reverse_each do |callback| callbacks = callback.apply(callbacks) end method << callbacks method << "value" method.join("\n") end # ... end

Slide 173

Slide 173 text

active_support/callbacks.rb: class CallbackChain < Array #:nodoc:# # ... def compile method = ["value = nil", "halted = false"] callbacks = "value = !halted && (!block_given? || yield)" reverse_each do |callback| callbacks = callback.apply(callbacks) end method << callbacks method << "value" method.join("\n") end # ... end

Slide 174

Slide 174 text

def apply(code) case @kind when :before <<-RUBY_EVAL if !halted && #{@compiled_options} # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = #{@filter} halted = (#{chain.config[:terminator]}) if halted halted_callback_hook(#{@raw_filter.inspect.inspect}) end end #{code} RUBY_EVAL when :after <<-RUBY_EVAL #{code} if #{!chain.config[:skip_after_callbacks_if_terminated] || "!halted"} && #{@compiled_options} #{@filter} end RUBY_EVAL when :around name = define_conditional_callback <<-RUBY_EVAL active_support/callbacks.rb:

Slide 175

Slide 175 text

def apply(code) case @kind when :before <<-RUBY_EVAL if !halted && #{@compiled_options} # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = #{@filter} halted = (#{chain.config[:terminator]}) if halted halted_callback_hook(#{@raw_filter.inspect.inspect}) end end #{code} RUBY_EVAL when :after <<-RUBY_EVAL #{code} if #{!chain.config[:skip_after_callbacks_if_terminated] || "!halted"} && #{@compiled_options} #{@filter} end RUBY_EVAL when :around name = define_conditional_callback <<-RUBY_EVAL active_support/callbacks.rb:

Slide 176

Slide 176 text

def apply(code) case @kind when :before <<-RUBY_EVAL if !halted && #{@compiled_options} # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = #{@filter} halted = (#{chain.config[:terminator]}) if halted halted_callback_hook(#{@raw_filter.inspect.inspect}) end end #{code} RUBY_EVAL when :after <<-RUBY_EVAL #{code} if #{!chain.config[:skip_after_callbacks_if_terminated] || "!halted"} && #{@compiled_options} #{@filter} end RUBY_EVAL when :around name = define_conditional_callback <<-RUBY_EVAL active_support/callbacks.rb:

Slide 177

Slide 177 text

def apply(code) case @kind when :before <<-RUBY_EVAL if !halted && #{@compiled_options} # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = #{@filter} halted = (#{chain.config[:terminator]}) if halted halted_callback_hook(#{@raw_filter.inspect.inspect}) end end #{code} RUBY_EVAL when :after <<-RUBY_EVAL #{code} if #{!chain.config[:skip_after_callbacks_if_terminated] || "!halted"} && #{@compiled_options} #{@filter} end RUBY_EVAL when :around name = define_conditional_callback <<-RUBY_EVAL active_support/callbacks.rb:

Slide 178

Slide 178 text

halted = (#{chain.config[:terminator]}) if halted halted_callback_hook(#{@raw_filter.inspect.inspect}) end end #{code} RUBY_EVAL when :after <<-RUBY_EVAL #{code} if #{!chain.config[:skip_after_callbacks_if_terminated] || "!halted"} && #{@compiled_options} #{@filter} end RUBY_EVAL when :around name = define_conditional_callback <<-RUBY_EVAL #{name}(halted) do #{code} value end RUBY_EVAL end end

Slide 179

Slide 179 text

active_support/callbacks.rb: class Callback #:nodoc:# @@_callback_sequence = 0 attr_accessor :chain, :filter, :kind, :options, :klass, :raw_filter def initialize(chain, filter, kind, options, klass) @chain, @kind, @klass = chain, kind, klass deprecate_per_key_option(options) normalize_options!(options) @raw_filter, @options = filter, options @filter = _compile_filter(filter) recompile_options! end # ... end

Slide 180

Slide 180 text

active_support/callbacks.rb: class Callback #:nodoc:# @@_callback_sequence = 0 attr_accessor :chain, :filter, :kind, :options, :klass, :raw_filter def initialize(chain, filter, kind, options, klass) @chain, @kind, @klass = chain, kind, klass deprecate_per_key_option(options) normalize_options!(options) @raw_filter, @options = filter, options @filter = _compile_filter(filter) recompile_options! end # ... end

Slide 181

Slide 181 text

active_support/callbacks.rb: class Callback #:nodoc:# @@_callback_sequence = 0 attr_accessor :chain, :filter, :kind, :options, :klass, :raw_filter def initialize(chain, filter, kind, options, klass) @chain, @kind, @klass = chain, kind, klass deprecate_per_key_option(options) normalize_options!(options) @raw_filter, @options = filter, options @filter = _compile_filter(filter) recompile_options! end # ... end

Slide 182

Slide 182 text

active_support/callbacks.rb: def recompile_options! conditions = ["true"] unless options[:if].empty? conditions << Array(_compile_filter(options[:if])) end unless options[:unless].empty? conditions << Array(_compile_filter(options[:unless])). map {|f| "!#{f}"} end @compiled_options = conditions.flatten.join(" && ") end

Slide 183

Slide 183 text

active_support/callbacks.rb: def recompile_options! conditions = ["true"] unless options[:if].empty? conditions << Array(_compile_filter(options[:if])) end unless options[:unless].empty? conditions << Array(_compile_filter(options[:unless])). map {|f| "!#{f}"} end @compiled_options = conditions.flatten.join(" && ") end

Slide 184

Slide 184 text

def _compile_filter(filter) @_is_object_filter = false case filter when Array filter.map {|f| _compile_filter(f)} when Symbol filter when String "(#{filter})" when Proc method_name = "_callback_#{@kind}_#{next_id}" @klass.send(:define_method, method_name, &filter) return method_name if filter.arity <= 0 method_name << (filter.arity == 1 ? "(self)" : " self, Proc.new ") else method_name = _method_name_for_object_filter(kind, filter) @_is_object_filter = true @klass.send(:define_method, "#{method_name}_object") { filter } _normalize_legacy_filter(kind, filter) scopes = Array(chain.config[:scope]) method_to_call = scopes.map{ |s| s.is_a?(Symbol) ? send(s) : s }.join("_") @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{method_name}(&blk) active_support/callbacks.rb:

Slide 185

Slide 185 text

when String "(#{filter})" when Proc method_name = "_callback_#{@kind}_#{next_id}" @klass.send(:define_method, method_name, &filter) return method_name if filter.arity <= 0 method_name << (filter.arity == 1 ? "(self)" : " self, Proc.new ") else method_name = _method_name_for_object_filter(kind, filter) @_is_object_filter = true @klass.send(:define_method, "#{method_name}_object") { filter } _normalize_legacy_filter(kind, filter) scopes = Array(chain.config[:scope]) method_to_call = scopes.map{ |s| s.is_a?(Symbol) ? send(s) : s }.join("_") @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{method_name}(&blk) #{method_name}_object.send(:#{method_to_call}, self, &blk) end RUBY_EVAL method_name end end

Slide 186

Slide 186 text

value = nil halted = false if !halted && true # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = _callback_before_15 halted = (result == false) if halted halted_callback_hook("#") end end if !halted && true # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = say_my_name halted = (result == false) if halted halted_callback_hook(":say_my_name") end end if !halted && true # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = _callback_before_17(self)

Slide 187

Slide 187 text

if !halted && true # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = _callback_before_17(self) halted = (result == false) if halted halted_callback_hook("#") end end if !halted && true # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = (puts "My name is #{self.name}") halted = (result == false) if halted halted_callback_hook("\"puts \\\"My name is \\\#{self.name}\\\"\"") end end value = !halted && (!block_given? || yield) value

Slide 188

Slide 188 text

if !halted && true # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = _callback_before_17(self) halted = (result == false) if halted halted_callback_hook("#") end end if !halted && true # This double assignment is to prevent warnings in 1.9.3 as # the `result` variable is not always used except if the # terminator code refers to it. result = result = (puts "My name is #{self.name}") halted = (result == false) if halted halted_callback_hook("\"puts \\\"My name is \\\#{self.name}\\\"\"") end end value = !halted && (!block_given? || yield) value

Slide 189

Slide 189 text

active_support/callbacks.rb: module ClassMethods def __define_callbacks(kind, object) #:nodoc: name = __callback_runner_name(kind) unless object.respond_to?(name, true) str = object.send("_#{kind}_callbacks").compile class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{name}() #{str} end protected :#{name} RUBY_EVAL end name end end def run_callbacks(kind, &block) runner_name = self.class.__define_callbacks(kind, self) send(runner_name, &block) end

Slide 190

Slide 190 text

active_record/callbacks.rb: module Callbacks def destroy #:nodoc: run_callbacks(:destroy) { super } end def touch(*) #:nodoc: run_callbacks(:touch) { super } end private def create_or_update #:nodoc: run_callbacks(:save) { super } end def create_record #:nodoc: run_callbacks(:create) { super } end def update_record(*) #:nodoc: run_callbacks(:update) { super } end end

Slide 191

Slide 191 text

A NOVEL IDEA!

Slide 192

Slide 192 text

INHERITANCE AND SUPER

Slide 193

Slide 193 text

BEFORE FILTER!!! class User < ActiveRecord::Base def create_or_update say_my_name && super end def say_my_name puts "My name is #{name}"; true end end

Slide 194

Slide 194 text

BEFORE FILTER!!! class User < ActiveRecord::Base def create_or_update say_my_name && super end def say_my_name puts "My name is #{name}"; true end end

Slide 195

Slide 195 text

AFTER FILTER!!! class User < ActiveRecord::Base def create_or_update super && final_stuff or raise ActiveRecord::Rollback end def final_stuff puts "IMPORTANT FINAL STUFF" end end

Slide 196

Slide 196 text

AFTER FILTER!!! class User < ActiveRecord::Base def create_or_update super && final_stuff or raise ActiveRecord::Rollback end def final_stuff puts "IMPORTANT FINAL STUFF" end end

Slide 197

Slide 197 text

AROUND FILTER!!! class User < ActiveRecord::Base def create_or_update puts "GET READY" unless sneak_attack? super puts "WASN'T THAT EXCITING?" if awesome? end end

Slide 198

Slide 198 text

AROUND FILTER!!! class User < ActiveRecord::Base def create_or_update puts "GET READY" unless sneak_attack? super puts "WASN'T THAT EXCITING?" if awesome? end end

Slide 199

Slide 199 text

“That's a total of twelve callbacks, which gives you immense power to react and prepare for each state in the Active Record life cycle.” ― active_record/callbacks.rb

Slide 200

Slide 200 text

“That’s Ruby, which gives you immense power to do, you know, pretty much anything, ever.” ― me

Slide 201

Slide 201 text

SO?

Slide 202

Slide 202 text

ACTIVERECORD WORKS WELL ENOUGH

Slide 203

Slide 203 text

IT’S BIG AND COMPLEX, CHANGE WILL BE HARD

Slide 204

Slide 204 text

CHANGES WILL INTENTIONALLY BREAK MY STUFF

Slide 205

Slide 205 text

CHANGES WILL ACCIDENTALLY BREAK MY STUFF

Slide 206

Slide 206 text

USE SOMETHING ELSE WHEN YOU NEED TO

Slide 207

Slide 207 text

LEGACY ACTIVERECORD IS

Slide 208

Slide 208 text

LEGACY ACTIVERECORD IS OUR

Slide 209

Slide 209 text

IT DESERVES BETTER

Slide 210

Slide 210 text

NEW USERS DESERVE BETTER

Slide 211

Slide 211 text

WE CAN DO BETTER

Slide 212

Slide 212 text

WRITE SOME DOCS

Slide 213

Slide 213 text

WRITE SOME TESTS

Slide 214

Slide 214 text

TRIAGE SOME ISSUES

Slide 215

Slide 215 text

PARTICIPATE IN THE DISCUSSION

Slide 216

Slide 216 text

BETTER STARTS NOW.

Slide 217

Slide 217 text

Ernie Miller / @erniemiller / http://erniemiller.org THANK YOU!