Save 37% off PRO during our Black Friday Sale! »

Data Integrity

Data Integrity

Be confident in your data. Less bugs. Moar happy.

Delivered at hsv.rb - http://www.meetup.com/hsv-rb/events/192571982/

1433d0176e764f9ce48f70673baa72e8?s=128

Jay Hayes

July 24, 2014
Tweet

Transcript

  1. EBUBJOUFHSJUZ ITWSC

  2. +BZ)BZFT !JBNWFSZ

  3. JBNWFSZDPN

  4. None
  5. None
  6. JNNFSTJWFCPPUDBNQT DPOTVMUJOH XSJUJOH

  7. None
  8. None
  9. EBUBJOUFHSJUZ

  10. EBUBJOUFHSJUZ

  11. EBUBJOUFHSJUZ

  12. EBUBJOUFHSJUZ

  13. USVTUZPVSEBUB

  14. UIFEBUBT create_table :products do |t| t.string :description t.boolean :active t.decimal

    :price end
  15. XIBUUIFTUBUF product = Product.all.sample product.description => ?

  16. XIBUUIFTUBUF product = Product.all.sample product.description => “doge cannon”

  17. XIBUUIFTUBUF product = Product.all.sample product.description => ‘’

  18. XIBUUIFTUBUF product = Product.all.sample product.description => nil WOMP

  19. XIBUUIFTUBUF product = Product.all.sample product.active => ?

  20. XIBUUIFTUBUF product = Product.all.sample product.active => true

  21. XIBUUIFTUBUF product = Product.all.sample product.active => false

  22. XIBUUIFTUBUF product = Product.all.sample product.active => nil WAT

  23. product = Product.new product.description.upcase NoMethodError: undefined method `upcase` for nil:NilClass

  24. product = Product.new product.description.to_s.upcase

  25. product.price * TAX_RATE NoMethodError: undefined method `*` for nil:NilClass

  26. (product.price || 0) * TAX_RATE

  27. XIBUJTOPUIJOH

  28. DPOTUSBJOUT

  29. t.string :description, null: false t.boolean :active, null: false t.decimal :price,

    null: false
  30. USVTU

  31. product = Product.new product.description.upcase => “KITTEH GOOGLEH EYES” &WFSZ 4JOHMF

    5JNF
  32. TFOTJCMF EFGBVMUT

  33. TUSJOHT t.string :description, null: false, default: ‘’

  34. CPPMFBOT t.boolean :active, null: false, default: false

  35. EFDJNBMT t.decimal :price, null: false

  36. NPBSDPOTUSBJOUT 

  37. TUSJOHT t.string :description, null: false, default: ‘’, limit: 100

  38. EFDJNBMT t.boolean :active, null: false, scale: 2, precision: 5

  39. VOJRVFOFTT

  40. UIFEBUBT create_table :users do |t| t.string :email, null: false end

  41. User.create(email: “jay@bignerdranch.com”) User.create(email: “jay@bignerdranch.com”) User.all => [#<User id: 1, email:

    “jay…”>, #<User id: 2, email: “jay…”>] who even are you???
  42. validate :email, uniqueness: true

  43. WBMJEBUJPOT  EPOUDVUJU

  44. robots.thoughtbot.com/the-perils-of-uniqueness-validations

  45. JOEJDFT create_table :users do |t| t.string :email, null: false t.index

    :email, unique: true end
  46. SFGFSFODJBM JOUFHSJUZ

  47. UIFEBUBT create_table :purchases do |t| t.integer :product_id, null: false t.integer

    :user_id, null: false t.index %i(product_id, user_id), unique: true end
  48. user = User.find(1) product = Product.find(1) Purchase.create(user: user, product: product)

    product.destroy Purchase.last => #<Purchase id: 1, product_id: 1, user_id: 1> _.product => nil but it dead???
  49. dependent: :destroy dependent: :delete dependent: :delete_all

  50. HSFBU  VOUJMJUJTOU

  51. user = User.find(1) product = Product.find(1) Purchase.create(user: user, product: product)

    product.delete Purchase.last => #<Purchase id: 1, product_id: 1, user_id: 1> WOMP
  52. HFNJOTUBMMGPSFJHOFS create_table :purchases do |t| # … t.foreign_key :products, dependent:

    :delete t.foreign_key :users, dependent: :restrict end
  53. None
  54. SBJMTSBJMT

  55. CFDPOGJEFOU

  56. BWPJECVHT

  57. IBWFJOUFHSJUZ

  58. bignerdranch.com/blog/coding-rails-with-data-integrity ! bignerdranch.com/blog/coding-rails-with-data-integrity-part-2 ! bignerdranch.com/blog/coding-rails-with-data-integrity-part-3 CMPHT

  59. 5IBOLT !JBNWFSZ