blow up when things are wrong

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

Upload: vesa-vaenskae

Post on 26-Jun-2015

85 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Blow up when things are wrong

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

When something is wrong, you should know

about it as fast as possible

Page 3: Blow up when things are wrong

#save vs #save!

Page 4: Blow up when things are wrong

foo.save!

# or

if foo.save # successelse # failend

Page 5: Blow up when things are wrong

The case of updating attributes

Page 6: Blow up when things are wrong

Validations Callbacks

#update_attributes ✔ ✔

#update_attribute ✖ ✔

Page 7: Blow up when things are wrong

Question

Page 8: Blow up when things are wrong

Which of #decrement, #decrement!, and

#decrement_counter skip validations/

callbacks?

Page 9: Blow up when things are wrong

Validations Callbacks

#decrement ✔ ✖

#decrement! ✖ ✔

#decrement_counter ✖ ✖

Page 12: Blow up when things are wrong

Making it simpler

Page 14: Blow up when things are wrong

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://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

I

Page 17: Blow up when things are wrong

What is thecurrent status?

Page 18: Blow up when things are wrong

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

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

Useless ActiveResource information

•ActiveResource #update_attribute will run validations normally

Page 21: Blow up when things are wrong

Extra Ruby tip

Page 22: Blow up when things are wrong

{foo: "bar"}[:foo]

{foo: "bar"}.fetch(:foo)

Page 23: Blow up when things are wrong

{foo: "bar"}[:foo] #=> "bar"

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

Page 24: Blow up when things are wrong

{foo: "bar"}[:whatevs]

{foo: "bar"}.fetch(:whatevs)

Page 25: Blow up when things are wrong

{foo: "bar"}[:whatevs] #=> nil

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

Page 26: Blow up when things are wrong

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

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