Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best development practices to follow #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 2016-09-09.md
Original file line number Diff line number Diff line change
@@ -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
~~~