Today I Learned

TIL, 2017-02-26

  • Ruby benchmark class. Apparently, catching AR::RecordInvalid is super slow!
  • However, if you don’t expect to hit the exception often, then you can still wrap transactions just in case.
  • The idea about design patterns is that you don’t read one and then try to use it immediately. You read a lot, don’t understand them that much, but keep them in your head because you might need them at some point.
  • Pre-logic hooks methods work well in service classes where you need to make sure of some things before running the actual code, at least you avoid if statement hell.
  • Didn’t know about Virtual Account Proxy. This can be useful if you don’t want to execute immediately.
  • Revisit AR updating attributes reference

    # These are the same, they don't change the attribute in the database yet (need to save)
    user.name = 'Rob' and user.write_attribute(:name, 'Rob')
    
    # This method will set all the attributes you pass it.
    # The changes are not saved to the database.
    user.attributes = { name: 'Rob', age: 12 }
    
    # This will change the attribute in the model and skip database validations
    # Any other changed attributes are also saved
    user.update_attribute(:name, 'Rob')
    
    # Check attributes, check validations, updates the record in the databae
    user.update or user.update_attributes
    
    # Execute a direct SQL UPDATE and skips validations and callbacks.
    # Raises an exception if any of the columns are readonly.
    user.update_columns(name: 'Rob') or user.update_column(:name, 'Rob')
    
    # Find and update, runs validations
    User.update(1, name: 'Rob')
    
    # SQL UPDATE, no validations or callbacks
    User.where(name: 'Robbie').update_all(name: 'Rob')
    

This project is maintained by daryllxd