Blow up when things are wrong

235 views

Published on

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

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
235
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Blow up when things are wrong

  1. 1. Blow up whenthings are wrongActiveRecord editionVesa Vänskä, Kisko LabsHelsinki Ruby Brigade, Rails Girls
  2. 2. When somethingis wrong, youshould knowabout it as fast aspossible
  3. 3. #save vs #save!
  4. 4. foo.save!# orif foo.save# successelse# failend
  5. 5. The case ofupdatingattributes
  6. 6. Validations Callbacks#update_attributes ✔ ✔#update_attribute ✖ ✔
  7. 7. Question
  8. 8. Which of #decrement,#decrement!, and#decrement_counterskip validations/callbacks?
  9. 9. Validations Callbacks#decrement ✔ ✖#decrement! ✖ ✔#decrement_counter ✖ ✖
  10. 10. http://guides.rubyonrails.org/active_record_validations_callbacks.html
  11. 11. ATKDo Not WantRubyGemhttps://github.com/garybernhardt/do_not_want
  12. 12. Making it simpler
  13. 13. In Rails 3.2.7 the#update_attribute method wasactually deprecated
  14. 14. 14.6.2012 – #update_attribute is deprecated in 3.2branch14.6.2012 – #update_attribute was removed from 4.0branch24.7.2012 – #update_column was deprecated in 4.0branch26.7.2012 – Rails 3.2.7 release30.7.2012 – #update_column is undeprecated in 4.0branch1.8.2012 – #update_attribute is undeprecated in 3.2branch25.8.2012 – #update_attribute is put back in 4.0branch9.8.2012 – Rails 3.2.8 release
  15. 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/81542f95d25825a7d3eff87d6f706661bf553b18Links
  16. 16. I
  17. 17. What is thecurrent status?
  18. 18. After all this•In Rails 4 the recommended wayis to use #update with a hash•#update_attributes is aliased to#update•#update_attribute is still skippingvalidations
  19. 19. Vesas laws ofupdating attributes1. Use #update_attributes or usesetters and #save/#save!2. All other methods of updatingattributes need an accompanyingcomment that explains why youdidnt follow the first rule.
  20. 20. Useless ActiveResourceinformation•ActiveResource #update_attributewill run validations normally
  21. 21. Extra Ruby tip
  22. 22. {foo: "bar"}[:foo]{foo: "bar"}.fetch(:foo)
  23. 23. {foo: "bar"}[:foo] #=> "bar"{foo: "bar"}.fetch(:foo)#=> "bar"
  24. 24. {foo: "bar"}[:whatevs]{foo: "bar"}.fetch(:whatevs)
  25. 25. {foo: "bar"}[:whatevs] #=> nil{foo: "bar"}.fetch(:whatevs)#=> KeyError: key notfound: :whatevs
  26. 26. {foo: "bar"}.fetch(:whatevs, :not_found)#=> :not_found{foo: "bar"}.fetch(:whatevs) { 1 + 1 }#=> 2
  27. 27. Thank you!twitter.com/vesanvesavanska.com

×