a guest Dec 13th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Let's say user wants to charge a scooter.
  2. class Charge
  3.   belongs_to :user
  4.   belongs_to :scooter
  5. end
  7. # we want to ensure that scooter is discharged
  8. class Charge
  9.   belongs_to :user
  10.   belongs_to :scooter
  11.   validate do |charge|
  12.     charge.scooter.battery < 0.3 #or whatever magic number
  13.   end
  14. end
  16. user, scooter: scooter)
  18. # that makes sense but when scooter changes this object is no longer valid.
  19. # when objects become invalid overtime, things got messed up.
  21. # Point:
  22. # Validation could not depend on external objects.
  23. # When there is more than 1 object in interaction, you need ensure
  24. # that they could work together as a whole and it should be validated inside
  25. # the application business logic/use case class but not in the model itself.
  27. # Why does this point matter?
  28. # Rails validation is another thing that have a tendency of being overused.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand