Notes for Ror Project

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]


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

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

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

Make new branch for every new task.

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

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

  bundle update
  bundle install --without production

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

  git mv README.rdoc

then commite it.

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 { = email.downcase }

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


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:

  => 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.

Must mention the Gem version.

Here are two strings: first_name and last_name

  first_name + ' ' + last_name


  "#{first_name} #{last_name}"

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 ——————————


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s