Oomoo

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”) 
    quote_value
    template 
     
     

Controllers 

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

Views 

    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   
     
    /app/views/orders/index.rhtml 
    Helper:
          /app/helpers/orders_helper.rb 
          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. 
     
About these ads

2 Comments »

  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.

    bruno

    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 )

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

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: