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

Blow up when things are wrong

Blow up when things are wrong

When something unexpected happens, you should know about it immediately to make debugging and fixing the problem easier. Discusses ActiveRecord persistence APIs.

Vesa Vänskä

May 13, 2013
Tweet

More Decks by Vesa Vänskä

Other Decks in Programming

Transcript

  1. Blow up when
    things are wrong
    ActiveRecord edition
    Vesa Vänskä, Kisko Labs
    Helsinki Ruby Brigade, Rails Girls

    View Slide

  2. When something
    is wrong, you
    should know
    about it as fast as
    possible

    View Slide

  3. #save vs #save!

    View Slide

  4. foo.save!
    # or
    if foo.save
    # success
    else
    # fail
    end

    View Slide

  5. The case of
    updating
    attributes

    View Slide

  6. Validations Callbacks
    #update_attributes ✔ ✔
    #update_attribute ✖ ✔

    View Slide

  7. Question

    View Slide

  8. Which of #decrement,
    #decrement!, and
    #decrement_counter
    skip validations/
    callbacks?

    View Slide

  9. Validations Callbacks
    #decrement ✔ ✖
    #decrement! ✖ ✔
    #decrement_counter ✖ ✖

    View Slide

  10. http://guides.rubyonrails.org/
    active_record_validations_callba
    cks.html

    View Slide

  11. ATK
    Do Not Want
    RubyGem
    https://github.com/
    garybernhardt/do_not_want

    View Slide

  12. Making it simpler

    View Slide

  13. In Rails 3.2.7 the
    #update_attribute method was
    actually deprecated

    View Slide

  14. 14.6.2012 – #update_attribute is deprecated in 3.2
    branch
    14.6.2012 – #update_attribute was removed from 4.0
    branch
    24.7.2012 – #update_column was deprecated in 4.0
    branch
    26.7.2012 – Rails 3.2.7 release
    30.7.2012 – #update_column is undeprecated in 4.0
    branch
    1.8.2012 – #update_attribute is undeprecated in 3.2
    branch
    25.8.2012 – #update_attribute is put back in 4.0
    branch
    9.8.2012 – Rails 3.2.8 release

    View Slide

  15. • https://github.com/rails/rails/commit/
    b081f6b59fb3f15d12043072ad9b331ffd2bc56e
    • https://github.com/rails/rails/pull/6738
    • https://github.com/rails/rails/pull/1190
    • https://github.com/rails/rails/commit/
    4ac81de52fbcdabc68f6d1fa8a5ee9ff7fff7df1
    • https://github.com/rails/rails/commit/
    50bdb924ba26999a468ec4844917cefec39ba08c
    • https://github.com/rails/rails/commit/
    81542f95d25825a7d3eff87d6f706661bf553b18
    Links

    View Slide

  16. I

    View Slide

  17. What is the
    current status?

    View Slide

  18. After all this
    •In Rails 4 the recommended way
    is to use #update with a hash
    •#update_attributes is aliased to
    #update
    •#update_attribute is still skipping
    validations

    View Slide

  19. Vesa's laws of
    updating attributes
    1. Use #update_attributes or use
    setters and #save/#save!
    2. All other methods of updating
    attributes need an accompanying
    comment that explains why you
    didn't follow the first rule.

    View Slide

  20. Useless ActiveResource
    information
    •ActiveResource #update_attribute
    will run validations normally

    View Slide

  21. Extra Ruby tip

    View Slide

  22. {foo: "bar"}[:foo]
    {foo: "bar"}.fetch(:foo)

    View Slide

  23. {foo: "bar"}[:foo] #=> "bar"
    {foo: "bar"}.fetch(:foo)
    #=> "bar"

    View Slide

  24. {foo: "bar"}[:whatevs]
    {foo: "bar"}.fetch(:whatevs)

    View Slide

  25. {foo: "bar"}[:whatevs] #=> nil
    {foo: "bar"}.fetch(:whatevs)
    #=> KeyError: key not
    found: :whatevs

    View Slide

  26. {foo: "bar"}
    .fetch(:whatevs, :not_found)
    #=> :not_found
    {foo: "bar"}
    .fetch(:whatevs) { 1 + 1 }
    #=> 2

    View Slide

  27. Thank you!
    twitter.com/vesan
    vesavanska.com

    View Slide