Guest User

Untitled

a guest
Dec 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. # Let's say user wants to charge a scooter.
  2. class Charge
  3. belongs_to :user
  4. belongs_to :scooter
  5. end
  6.  
  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
  15.  
  16. Charge.new(user: user, scooter: scooter)
  17.  
  18. # that makes sense but when scooter changes this object is no longer valid.
  19. # when objects become invalid overtime, things got messed up.
  20.  
  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.
  26.  
  27. # Why does this point matter?
  28. # Rails validation is another thing that have a tendency of being overused.
Add Comment
Please, Sign In to add comment