March 11, 2008

Some Rails Naming Conventions

Filed under: rails — oomoo @ 1:43 pm

Just notes about some of the naming conventions used by Rails.  I pulled this info from various sources and filled in the rest.

Model/Class Table/Schema 

    Table (in database) – Plural, with underscores instead of spaces between words  (ie: steering_wheels)
    Model / Class – Singular, first letter Capitalized, CamelCase for models (ie: SteeringWheel) 
    Model/Class      Table/Schema
    Order                orders
    LineItem            line_items
    Person               people
    Address             addresses
    Legacy               legacies
    Mouse               mice 
    *Foreign key fields are named…  tablename_id     (i.e.  customers_id, orders_id) 
    *id  (Every table automatically gets a key field of “id”  (just plain old “id”)) 
    *created_at, created_on, updated_at, updated_on  (IF USED, rails will automatically update these fields) 
    *lock_version  (IF USED, automatic field used for “optimistic locking”)  (integer, default=0, null=false) 
    *counter_cache  (field used to save database lookups) 
    *type  (IF USED, allows “single table inheritence”) 
    *mytable_count  (IF USED, allows rails to calculate the # of objects created for the class) (when you define it, make it default to zero) 
    *position    (IF USED, “position” is used when sorting objects in a “acts_as_list” class) 
    *parent_id    (IF USED, foreign key column in an “acts_as_tree” model) 
    *lft and rgt    (IF USED, provides nested set enhanced tree-like functionality “acts_as_nested_set”) 


    Class Name           File Name   
    WeblogsController        weblogs_controller.rb
    LineItemsController  line_items_controller.rb 


    Folder Name       File Name                                  
    web_logs            index.rhtml  (this is just the default, any number of html files can exist here)
    line_items           index.rhtml   
          Module Name = OrdersHelper 
    To access the view from a browser, if you are using index.rhtml, you just need to specify the view folder name:
    *If you create a file at app/views/layouts/application.rhtml it will automatically be used as a wrapper around all other layouts. 


  1. […] to roll back to a prior version!  See my “Naming Conventions” Post for more info:  https://oomoo.wordpress.com/2008/03/11/some-rails-naming-conventions/  […]

    Pingback by Create A New Migration Script « Oomoo — March 20, 2008 @ 7:32 pm

  2. Thanks for that ! I was looking for it since a while. This Friday 13 was a good day.


    Comment by bruno duyé — June 13, 2008 @ 4:53 pm

RSS feed for comments on this post. TrackBack URI

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 )

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

Blog at WordPress.com.

%d bloggers like this: