Download - Blow up when things are wrong
![Page 1: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/1.jpg)
Blow up when things are wrong
ActiveRecord edition
Vesa Vänskä, Kisko LabsHelsinki Ruby Brigade, Rails Girls
![Page 2: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/2.jpg)
When something is wrong, you should know
about it as fast as possible
![Page 3: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/3.jpg)
#save vs #save!
![Page 4: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/4.jpg)
foo.save!
# or
if foo.save # successelse # failend
![Page 5: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/5.jpg)
The case of updating attributes
![Page 6: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/6.jpg)
Validations Callbacks
#update_attributes ✔ ✔
#update_attribute ✖ ✔
![Page 7: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/7.jpg)
Question
![Page 8: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/8.jpg)
Which of #decrement, #decrement!, and
#decrement_counter skip validations/
callbacks?
![Page 9: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/9.jpg)
Validations Callbacks
#decrement ✔ ✖
#decrement! ✖ ✔
#decrement_counter ✖ ✖
![Page 10: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/10.jpg)
http://guides.rubyonrails.org/active_record_validations_callba
cks.html
![Page 11: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/11.jpg)
ATK
Do Not WantRubyGem
https://github.com/garybernhardt/do_not_want
![Page 12: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/12.jpg)
Making it simpler
![Page 13: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/13.jpg)
In Rails 3.2.7 the #update_attribute method was
actually deprecated
![Page 14: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/14.jpg)
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
![Page 15: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/15.jpg)
• 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
![Page 16: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/16.jpg)
I
![Page 17: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/17.jpg)
What is thecurrent status?
![Page 18: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/18.jpg)
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
![Page 19: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/19.jpg)
Vesa's laws ofupdating 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.
![Page 20: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/20.jpg)
Useless ActiveResource information
•ActiveResource #update_attribute will run validations normally
![Page 21: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/21.jpg)
Extra Ruby tip
![Page 22: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/22.jpg)
{foo: "bar"}[:foo]
{foo: "bar"}.fetch(:foo)
![Page 23: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/23.jpg)
{foo: "bar"}[:foo] #=> "bar"
{foo: "bar"}.fetch(:foo)#=> "bar"
![Page 24: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/24.jpg)
{foo: "bar"}[:whatevs]
{foo: "bar"}.fetch(:whatevs)
![Page 25: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/25.jpg)
{foo: "bar"}[:whatevs] #=> nil
{foo: "bar"}.fetch(:whatevs) #=> KeyError: key not found: :whatevs
![Page 26: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/26.jpg)
{foo: "bar"}.fetch(:whatevs, :not_found)#=> :not_found
{foo: "bar"}.fetch(:whatevs) { 1 + 1 }#=> 2
![Page 27: Blow up when things are wrong](https://reader033.vdocument.in/reader033/viewer/2022060119/558d1d2ed8b42a84258b472b/html5/thumbnails/27.jpg)
Thank you!twitter.com/vesanvesavanska.com