diff --git a/2016-09-09.md b/2016-09-09.md new file mode 100644 index 0000000..b2924bc --- /dev/null +++ b/2016-09-09.md @@ -0,0 +1,36 @@ +Harshwardhan Singh Rathore +========================== + +## Best Development Practices: +1. Never rename or edit the migration file which already has been merged into master instead create new migration to alter the table schema. + +2. Don't use underscore in css class name, use hyphen instead. For example: `class-name` not `class_name`. + +3. Always use html class to apply styling using css, never use element id to apply styling in css. + +4. Prepend class-name or element id by js if it is being used to manipulate using Javascript. + + For example: Use `js-element-id` or `js-class-name` + Instead of: `element-id` or `class-name` + +5. (Rails) Always use ActiveRecord transactions to bind database calls while running multiple database operation. +This will rollback all the changes to database if any one of the database query fails. +~~~ruby +ModelClass.transaction do + User.create!(email: params[:email], username: params[:username]) + Profile.create!(email: params[:email], name: params[:name]) +end +~~~ +In above mentioned case if `User` create query succeeds but `Profile` create fails then the user created by `User.create!(email: params[:email], username: params[:username])` +will be deleted. That's the beauty of using transactions that it rollbacks to the previous state if anything goes wrong. + +If you don't use transactions but want to achieve the same functionality then the code will look like this + +~~~ruby +user = User.create!(email: params[:email], username: params[:username]) +profile = Profile.create!(email: params[:email], name: params[:name]) + +unless profile do + user.destroy +end +~~~