Notes for Ror Project

1)
I use following command to create new project so that I can skip bundle, test files and set database:

rails new [project name] --skip-bundle --skip-test-unit --database=[database name]

or

rails new [project name] --skip-bundle -T -d [database name]

2)
try to use following command for genrating controller so that we can skip the test file:

  rails generate controller Users new --no-test-framework

3)
Make new branch for every new task.

4)
It good to use gem annotate, 2.5.0 for model annotation, then run following commands in your project through terminal:

    bundle install
    bundle exec annotate

5)
To install and include the new gems, we run bundle update and bundle install

  bundle update
  bundle install --without production

6)
Improved README file to be more helpful and descriptive, change it to use the Markdown extension .md using following command:

  git mv README.rdoc README.md

then commite it.

7)
Using validates :uniqueness does not guarantee uniqueness.
To ensure uniqueness do following:
In model write:

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

Now do following:

  rails generate migration add_index_to_users_email

then in migration file add following line

 add_index :users, :email

and add in model

  class User < ActiveRecord::Base
    before_save { self.email = email.downcase }
    .
    .
    .
  end

8)
Create shared folder in views(views/shared) for partials/view expected to be used in views across multiple controllers. Its rails convention.

9)

Use symbole instead of string in ActiveRecord Ordering.

See the difference:
When passing a string column name:

  Motel.order('name asc').to_sql
  => SELECT "motels".* FROM "motels" ORDER BY name asc

Now when passing a symbol column name:

  Motel.order(:name).to_sql
  => SELECT "motels".* FROM "motels" ORDER BY "motels"."name" ASC

When the symbol is passed, the query is built using fully qualified and properly quoted column names. Most of the time your query will run fine but this does matter! The issue comes up when you start joining tables or merging queries. Without the column being fully qualified with the table name, your query may produce an ambiguous match. This occurs when your joined tables have columns with the same name.

10)
Must mention the Gem version.

11)
Here are two strings: first_name and last_name
Bad:

  first_name + ' ' + last_name

Good:

  "#{first_name} #{last_name}"

12)
Rails loads all of the stylesheets all at once, we need a convention to limit controller-specific rules to pages associated with that controller. Using the controller_name as a class name is an easy way to accomplish that. e.g:

 <body class='<%= controller.controller_name %>'>

13) The truncate() helper is used to display limited characters of any description text. But before we call truncate() , we should called strip_tags() in order to remove the HTML tags from the description text.

<%= truncate(strip_tags(product.description), length: 80) %>

——————- In progess ——————————

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s