Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Elusive Attribute

The Elusive Attribute

Talk on the internals of attributes in ActiveModel and ActiveRecord at RailsConf 2019.

Video: https://www.youtube.com/watch?v=PNNrmNTQx2s
Summary: https://railsconf.com/program/sessions#session-751

References:
- Give GeneratedAttributeMethods module a name: https://github.com/rails/rails/pull/35595
- Make plain matcher match first, not last: https://github.com/rails/rails/pull/36005 (PR #36005, mentioned in slide 77)
- Velocipedia: https://www.behance.net/gallery/35437979/Velocipedia

Chris Salzberg

May 02, 2019
Tweet

More Decks by Chris Salzberg

Other Decks in Programming

Transcript

  1. As we know, there are known knowns; there are things

    we know we know. We also know there are known unknowns; that is to say we know there are some things we do not know. But there are also unknown unknowns - the ones we don't know we don't know. - Donald Rumsfeld United States Secretary of Defense (2003)
  2. People who are really into bikes [...] start out from

    the frame. They represent that small percentage of people who get the drawing perfectly right. Most other people tend to start from what they are most sure of, so for nearly everyone it’s the wheels. [...] I saw many people drawing all the rest in the hope that positioning handlebar, saddle and pedals would help them figure out the frame. - Gianluca Gimini
  3. @attributes Name: Chris Salzberg Handle: shioyama Born: Montreal, CA Based:

    Tokyo, JP Company: Degica Blog: dejimata.com Tags: Module Builder, Mobility
  4. class Post < ApplicationRecord end Post.ancestors => [Post, Post::GeneratedAssociationMethods, #<AR::AttributeMethods::GeneratedAttributeMethods:...>,

    ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, #<AR::AttributeMethods::GeneratedAttributeMethods:...>, AR::Base, ... ] < 6.0
  5. class Post < ApplicationRecord end Post.ancestors => [Post, Post::GeneratedAssociationMethods, #<AR::AttributeMethods::GeneratedAttributeMethods:...>,

    ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, #<AR::AttributeMethods::GeneratedAttributeMethods:...>, AR::Base, ... ] < 6.0
  6. class Post < ApplicationRecord end Post.ancestors => [Post, Post::GeneratedAssociationMethods, Post::GeneratedAttributeMethods,

    ApplicationRecord(abstract), ApplicationRecord::GeneratedAssociationMethods, ApplicationRecord::GeneratedAttributeMethods, ActiveRecord::Base, ... ] >= 6.0
  7. => [ ... :title_before_type_cast, :title_came_from_user?, :title?, :title_changed?, :title_change, :title_will_change!, :title_was,

    :title_previously_changed?, ... ] post = Post.new(title: "The Elusive Attribute") post.method(:title).owner.instance_methods Attribute Methods
  8. post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each

    do |meth| mod.send(:remove_method, meth) end mod.instance_methods #=> []
  9. post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each

    do |meth| mod.send(:remove_method, meth) end mod.instance_methods post.method(:title) # raises NameError: undefined method `title'
  10. post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each

    do |meth| mod.send(:remove_method, meth) end mod.instance_methods post.method(:title) post.title #=> ...
  11. post = Post.new(title: "The Elusive Attribute") mod = post.method(:title).owner mod.instance_methods.each

    do |meth| mod.send(:remove_method, meth) end mod.instance_methods post.method(:title) post.title #=> "The Elusive Attribute"
  12. cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post

    = Post.new post.method(:title=) post.title = "The Elusive Attribute" # raises ActiveModel::UnknownAttributeError
  13. cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post

    = Post.new post.method(:title=) post.title = "The Elusive Attribute" post.attributes #=> {}
  14. cache = AR::Base.connection.schema_cache columns_hash = cache.instance_variable_get(:@columns_hash) columns_hash["posts"] = {} post

    = Post.first post.method(:title) post.title post.attributes #=> {"id"=>1, "title"=>"The Elusive Attribute", # "created_at"=>2019-03-13 05:09:10 UTC, # "updated_at"=>2019-03-29 07:57:54 UTC}
  15. result = AR::Base.connection.exec_query( 'SELECT posts.* FROM posts') #=> #<ActiveRecord::Result:0x...> result.columns

    #=> ["id", "title", "created_at", "updated_at"] result.rows #=> [[1, "The Elusive Attribute", "2019-03-15...
  16. result = AR::Base.connection.exec_query( 'SELECT posts.* FROM posts') #=> #<ActiveRecord::Result:0x...> result.columns

    #=> ["id", "title", "created_at", "updated_at"] result.rows #=> [[1, "The Elusive Attribute", "2019-03-15... Post.column_names #=> []
  17. Constraints 1. Methods are fast, method missing is slow 2.

    schema is static, attributes are dynamic
  18. Constraints 1. Methods are fast, method missing is slow 2.

    schema is static, attributes are dynamic “front door” “back door”
  19. “Front Door” attribute_method_matchers.each do |matcher| method_name = matcher.method_name(attr_name) define_proxy_call true,

    generated_attribute_methods, method_name, matcher.target, attr_name.to_s end “title”
  20. “Front Door” attribute_method_matchers.each do |matcher| method_name = matcher.method_name(attr_name) define_proxy_call true,

    generated_attribute_methods, method_name, matcher.target, attr_name.to_s end “title_was” “attribute_was” “title”
  21. module ActiveModel::Dirty extend ActiveSupport::Concern include ActiveModel::AttributeMethods included do attribute_method_suffix "_changed?",

    "_change", "_will_change!", "_was" # Dispatch target for *_was attribute methods. def attribute_was(attr) attribute_changed?(attr) ? changed_attributes[attr] : _read_attribute(attr) end
  22. module ActiveModel::Dirty extend ActiveSupport::Concern include ActiveModel::AttributeMethods included do attribute_method_suffix "_changed?",

    "_change", "_will_change!", "_was" # Dispatch target for *_was attribute methods. def attribute_was(attr) attribute_changed?(attr) ? changed_attributes[attr] : _read_attribute(attr) end
  23. module ActiveModel::AttributeMethods included do class_attribute :attribute_method_matchers, instance_writer: false, default: [

    ClassMethods::AttributeMethodMatcher.new ] # ... end end def initialize(options = {}) @prefix = options.fetch(:prefix, "") @suffix = options.fetch(:suffix, "")
  24. module ActiveRecord module AttributeMethods module Read # ... def _read_attribute(attr_name,

    &b) @attributes.fetch_value(attr_name.to_s, &b) end alias :attribute :_read_attribute private :attribute end end end
  25. post = Post.select('title as foo').first post.foo #=> "The Elusive Attribute"

    post.foo = "This cannot be saved" post.foo_was post.foo_change
  26. post = Post.select('1 as one').first post.one #=> 1 post.one =

    2 # post.one #=> 2 post.one_was #=> 1 post.one_change #=> [1, 2]
  27. payments = Payment.select( 'amount, amount*0.08 as tax, amount*0.02 as tax_change')

    payment = payments.first payment.amount #=> 100 payment.tax #=> 8 payment.tax_change #=> ?
  28. payments = Payment.select( 'amount, amount*0.08 as tax, amount*0.02 as tax_change')

    payment = payments.first payment.amount #=> 100 payment.tax #=> 8 payment.tax_change #=> nil