Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EfeAgare/2cdd58cb03265363e79f0692d97ca616 to your computer and use it in GitHub Desktop.
Save EfeAgare/2cdd58cb03265363e79f0692d97ca616 to your computer and use it in GitHub Desktop.

Revisions

  1. @iangreenleaf iangreenleaf revised this gist Feb 21, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -147,3 +147,8 @@ Examples:
    * `app/views/bigfoot_sightings/index.html.erb`
    * `app/views/bigfoot_sightings/show.html.erb`
    * `app/views/profile/show.html.erb`

    # More resources #

    * [ActiveRecord naming and schema conventions (including magic column names)](http://edgeguides.rubyonrails.org/active_record_basics.html#naming-conventions)
    * [Mind map of Rails conventions](https://teddicodes.files.wordpress.com/2015/02/railsnamingconventions.pdf)
  2. @iangreenleaf iangreenleaf revised this gist Feb 20, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -113,7 +113,6 @@ end

    ```ruby
    # app/controllers/profiles_controller.rb
    # This controller is singular
    ProfilesController < ApplicationController
    def show
    # ...
    @@ -132,6 +131,8 @@ Example:

    ```ruby
    resources :bigfoot_sightings
    # Users can only see their own profiles, so we'll use `/profile` instead
    # of putting an id in the URL.
    resource :profile
    ```

  3. @iangreenleaf iangreenleaf revised this gist Feb 20, 2015. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -49,13 +49,13 @@ Model files go in `app/models/#{singular_model_name}.rb`.

    Example:

    ```
    ```ruby
    # app/models/bigfoot_sighting.rb
    class BigfootSighting < ActiveRecord::Base
    # This class will have these attributes: id, sighted_at, location
    end
    ```
    ```
    ```ruby
    # app/models/profile.rb
    class Profile < ActiveRecord::Base
    # Methods follow the same conventions as attributes
    @@ -73,14 +73,14 @@ Rails expects foreign keys in the database to have an `_id` suffix, and will map

    Example:

    ```
    ```ruby
    # app/models/bigfoot_sighting.rb
    class BigfootSighting < ActiveRecord::Base
    # This knows to use the profile_id field in the database
    belongs_to :profile
    end
    ```
    ```
    ```ruby
    # app/models/profile.rb
    class Profile < ActiveRecord::Base
    # This knows to look at the BigfootSighting class and find the foreign key in that table
    @@ -98,7 +98,7 @@ Controller files go in `app/controllers/#{resource_name}_controller.rb`.

    Example:

    ```
    ```ruby
    # app/controllers/bigfoot_sightings_controller.rb
    BigfootSightingsController < ApplicationController
    def index
    @@ -111,7 +111,7 @@ BigfootSightingsController < ApplicationController
    end
    ```

    ```
    ```ruby
    # app/controllers/profiles_controller.rb
    # This controller is singular
    ProfilesController < ApplicationController
    @@ -130,7 +130,7 @@ Route names are `snake_case`, and usually match the controller. Most of the time

    Example:

    ```
    ```ruby
    resources :bigfoot_sightings
    resource :profile
    ```
  4. @iangreenleaf iangreenleaf revised this gist Feb 20, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ Model *class names* use `CamelCase`. These are **singular**, and will map automa

    Model *attributes* and *methods* use `snake_case` and match the column names in the database.

    Model files go in `app/models/singular_model_name.rb`.
    Model files go in `app/models/#{singular_model_name}.rb`.

    Example:

    @@ -94,7 +94,7 @@ Controller *class names* use `CamelCase` and have `Controller` as a suffix. The

    Controller *actions* use `snake_case` and usually match the standard route names Rails defines (`index`, `show`, `new`, `create`, `edit`, `update`, `delete`).

    Controller files go in `app/controllers/resource_name_controller.rb`.
    Controller files go in `app/controllers/#{resource_name}_controller.rb`.

    Example:

    @@ -139,7 +139,7 @@ resource :profile

    View file names, by default, match the controller and action that they are tied to.

    Views go in `app/views/resource_name/action_name.html.erb`.
    Views go in `app/views/#{resource_name}/#{action_name}.html.erb`.

    Examples:

  5. @iangreenleaf iangreenleaf revised this gist Feb 20, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -83,6 +83,7 @@ end
    ```
    # app/models/profile.rb
    class Profile < ActiveRecord::Base
    # This knows to look at the BigfootSighting class and find the foreign key in that table
    has_many :bigfoot_sightings
    end
    ```
  6. @iangreenleaf iangreenleaf revised this gist Feb 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -59,7 +59,7 @@ end
    # app/models/profile.rb
    class Profile < ActiveRecord::Base
    # Methods follow the same conventions as attributes
    def veteran?
    def veteran_hunter?
    years_of_experience > 2
    end
    end
  7. @iangreenleaf iangreenleaf revised this gist Feb 20, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,17 @@
    # Rails naming conventions #

    ## General Ruby conventions ##

    Class names are `CamelCase`.

    Methods and variables are `snake_case`.

    Methods with a `?` suffix will return a boolean.

    Methods with a `!` suffix mean one of two things: either the method operates destructively in some fashion, or it will raise and exception instead of failing (such as Rails models' `#save!` vs. `#save`).

    In documentation, `::method_name` denotes a *class method*, while `#method_name` denotes a *instance method*.

    ## Database ##

    *Database tables* use `snake_case`. Table names are **plural**.
  8. @iangreenleaf iangreenleaf revised this gist Feb 20, 2015. 1 changed file with 106 additions and 6 deletions.
    112 changes: 106 additions & 6 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,31 @@

    ## Database ##

    *Database table names* use `snake_case`. Table names are **plural**.
    *Database tables* use `snake_case`. Table names are **plural**.

    *Column names* in the database use `snake_case`, but are generally **singular**.

    Example:

    ```
    +--------------------------+
    | bigfoot_sightings |
    +------------+-------------+
    | id | ID |
    | sighted_at | DATETIME |
    | location | STRING |
    | profile_id | FOREIGN KEY |
    +------------+-------------+
    +------------------------------+
    | profiles |
    +---------------------+--------+
    | id | ID |
    | name | STRING |
    | years_of_experience | INT |
    +---------------------+--------+
    ```

    ## Model ##

    Model *class names* use `CamelCase`. These are **singular**, and will map automatically to the plural database table name.
    @@ -14,22 +35,101 @@ Model *attributes* and *methods* use `snake_case` and match the column names in

    Model files go in `app/models/singular_model_name.rb`.

    Example:

    ```
    # app/models/bigfoot_sighting.rb
    class BigfootSighting < ActiveRecord::Base
    # This class will have these attributes: id, sighted_at, location
    end
    ```
    ```
    # app/models/profile.rb
    class Profile < ActiveRecord::Base
    # Methods follow the same conventions as attributes
    def veteran?
    years_of_experience > 2
    end
    end
    ```

    ### Relations in models ###

    Relations use `snake_case` and follow the type of relation, so `has_one` and `belongs_to` are **singular** while `has_many` is **plural**.

    Rails expects foreign keys in the database to have an `_id` suffix, and will map relations to those keys automatically if the names line up.

    Example:

    ```
    # app/models/bigfoot_sighting.rb
    class BigfootSighting < ActiveRecord::Base
    # This knows to use the profile_id field in the database
    belongs_to :profile
    end
    ```
    ```
    # app/models/profile.rb
    class Profile < ActiveRecord::Base
    has_many :bigfoot_sightings
    end
    ```

    ## Controllers ##

    Controller *class names* use `CamelCase` and have `Controller` as a suffix. The name of the controller is usually **plural**, unless it deals with a singular resource[^1]. The `Controller` suffix is always singular.
    Controller *class names* use `CamelCase` and have `Controller` as a suffix. The `Controller` suffix is always singular. The name of the resource is usually **plural**.

    Controller *actions* use `snake_case` and usually match the standard route names Rails defines.
    Controller *actions* use `snake_case` and usually match the standard route names Rails defines (`index`, `show`, `new`, `create`, `edit`, `update`, `delete`).

    Controller files go in `app/controllers/resource_name_controller.rb`.

    Example:

    ```
    # app/controllers/bigfoot_sightings_controller.rb
    BigfootSightingsController < ApplicationController
    def index
    # ...
    end
    def show
    # ...
    end
    # etc
    end
    ```

    ```
    # app/controllers/profiles_controller.rb
    # This controller is singular
    ProfilesController < ApplicationController
    def show
    # ...
    end
    # etc
    end
    ```

    ## Routes ##

    Route names are `snake_case`. They match the controller, so if the controller is plural then the route name will be plural and use the plural `resources`. If the controller is singular then the route name will be singular and will use the singular `resource`.
    Route names are `snake_case`, and usually match the controller. Most of the time routes are **plural** and use the plural `resources`.

    [Singular routes](http://edgeguides.rubyonrails.org/routing.html#singular-resources) are a special case. These use the singular `resource` and a singular resource name. However, they still map to a plural controller by default!

    Example:

    ```
    resources :bigfoot_sightings
    resource :profile
    ```

    ## Views ##

    View file names, by default, match the controller and action that they are tied to.

    Views go in `app/views/resource_name/action_name.erb.html`.
    Views go in `app/views/resource_name/action_name.html.erb`.

    Examples:

    [^1]: Singular resource controllers are used when there can logically be only one instance of a resource available. For example, we might use a singular `ProfileController` if users can only access their own profile.
    * `app/views/bigfoot_sightings/index.html.erb`
    * `app/views/bigfoot_sightings/show.html.erb`
    * `app/views/profile/show.html.erb`
  9. @iangreenleaf iangreenleaf created this gist Feb 20, 2015.
    35 changes: 35 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # Rails naming conventions #

    ## Database ##

    *Database table names* use `snake_case`. Table names are **plural**.

    *Column names* in the database use `snake_case`, but are generally **singular**.

    ## Model ##

    Model *class names* use `CamelCase`. These are **singular**, and will map automatically to the plural database table name.

    Model *attributes* and *methods* use `snake_case` and match the column names in the database.

    Model files go in `app/models/singular_model_name.rb`.

    ## Controllers ##

    Controller *class names* use `CamelCase` and have `Controller` as a suffix. The name of the controller is usually **plural**, unless it deals with a singular resource[^1]. The `Controller` suffix is always singular.

    Controller *actions* use `snake_case` and usually match the standard route names Rails defines.

    Controller files go in `app/controllers/resource_name_controller.rb`.

    ## Routes ##

    Route names are `snake_case`. They match the controller, so if the controller is plural then the route name will be plural and use the plural `resources`. If the controller is singular then the route name will be singular and will use the singular `resource`.

    ## Views ##

    View file names, by default, match the controller and action that they are tied to.

    Views go in `app/views/resource_name/action_name.erb.html`.

    [^1]: Singular resource controllers are used when there can logically be only one instance of a resource available. For example, we might use a singular `ProfileController` if users can only access their own profile.