-
Notifications
You must be signed in to change notification settings - Fork 4
Use the bang methods save!, create!, update!, destroy!
Always use the bang methods on AR objects unless you have a reason not to. Even then, use the bang methods.
I found this code in master:
after_create :create_profile_for_user
def create_profile_for_user
Profile.create(user: self)
end
What does this do? It tries to create a profile, silently ignoring any validation errors and then throws away the reference to the only object that has information about the error. Not what you want.
Instead, make it a habit to use the save!, create!, update!, destroy! methods and explicitly catch the exception if that is what you want to do. The exception you want is usually ActiveRecord::RecordInvalid
.
Or just let the exception pass to the controller where ApplicationController will do the right thing automatically for you (as of branch cancan https://github.com/fsek/web/blob/cancan/app/controllers/application_controller.rb#L14)
Have a nice day /Your friendy best-practices PSA guy (Written by @jforberg #93 https://github.com/fsek/web/issues/93)