This page's URI: http://www.geocities.com/dustinmarx/SW/rails/conventions.html
The Conventions of Rails
A central theme of the Ruby on Rails web development framework is to favor convention over configuration. This approach can be easier and more productive for developers only if the developers are familiar with these conventions. In fact, for someone new to a web framework, configuration files provide an obvious first place to look to understand the overall flow of the application. To be truly productive with Rails, one must be familiar with its assumptions and expected conventions. The purpose of this document is to provide a list of some of Rails conventions and methods for overriding the conventions when necessary.
Many of these conventions, assumptions, and the methods for overriding the conventions are detailed in the Rails API documentation. Many of Rails' conventions are related to database involvement and a good discussion on these and overcoming these when necessary is called Creating "HowToUseLegacySchemas". Another page regarding Rails naming conventions is Naming Conventions in Rails.
The Convention | Convention Description | Impacted Areas | Overriding the Convention | Additional Details |
---|---|---|---|---|
Ruby Class / DB Table ActiveRecord Mapping |
Name DB table with same name in plural form that Ruby model class has in singular form. | Model Class Name Database Table Name |
1. Use ActiveRecord::Base's set_table_name method to specify any table for your Ruby model class.2. Set ActiveRecord's pluralize_table_names attribute to false to have
singular version of table name identical to Ruby class name.3. Use ActiveRecord::Base's table_name_prefix and table_name_suffix
attributes to add convention-based table prefix and suffix respectively. |
ActiveRecord::Base API |
DB Table Primary Key | Database tables used in Rails assumed to have primary key named 'id'. | Database Primary Key Column Names | 1. Use ActiveRecord's set_primary_key method to
specify column other than 'id' as primary key.2. Use ActiveRecord's primary_key_prefix_type attribute
to add a prefix (potentially with underscore) in front of id for
primary column names. |
ActiveRecord::Base API |
Foreign Key Column Names | A database table used in Rails with a foreign key to another table
used in Rails should have its foreign key column name with the
format <otherModelName>_id . Notice that the
_id is prefixed with the singular version of the
name of the table the foreign key points to (which is usually the
Ruby model class' name). |
Database / ActiveRecord | You can have your dependent class use a differently named foreign key
column than the standard name described here. To explicitly specify
to the Ruby model class that its foreign key column is something else,
pass an options hash with the symbol :foreign_key
and the explicit foreign key name to the belongs_to
method. |
Naming Conventions
ActiveRecord::Associations |
Name of Join/Intersection Table | Join/Intersection tables are required for many-to-many relationships.
The name of such a "join" table in Rails follows the naming
convention of concatenating the names of each of the joined
tables together with an underscore between the tables' names.
Part of the convention is to list the table names in alphabetical
order. So, for example, if you joined tables PERSON and ADDRESS,
the join table should be named ADDRESS_PERSON . |
Database / ActiveRecord | To use a different name for the join/intersection table than the
conventional alphabetical order of joined tables with underscore,
pass an options hash with symbol :join_table and
the name you wish to explicitly specify for the join table to the
has_and_belongs_to_many method. |
ActiveRecord::Associations |
Database Sequence Names | Each DB table in Rails application needs corresponding sequence
name in form <table_name>_seq . |
Database Sequences | Use ActiveRecord's set_sequence_name method to specify
a different sequence for this particular ActiveRecord rather than the
one expected by default (<table_name>_seq ). |
ActiveRecord::Base API |
Template from Controller Action | If an action (method) in a child of ActionController::Base
does not explicitly define another action to invoke
(redirect_to )
or an explicit template for rendering (render ) the view,
it is assumed that there is a template view with the same name as the
action. |
View and Controller | The implicit assumption of a view template with name matching the
Controller's action (method) name will only be used if no
redirect_to or
render call is invoked in the action method. If you choose
to redirect_to another action or render an
explicitly named view, the implicit assumption of view with same name as
action is not used. |
ActionController::Base API |
Layout Files Locations/Names | The name of a layout file (.rhtml or .rxml ),
by convention, is expected to consist of the invoking controller's
name (without the _controller ). The template file with
format <controllerName>.rhtml or
<controllerName>.rxml is expected to be located in
the Rails application's app/views/layout directory. |
Views | You can explicitly specify the layout to be used by your controller
class (extends Explicitly specifying a layout in a controller class is one method
for sharing a single layout among multiple controllers in a given
application. If you want all controllers in your application to use
the same layout, you can have each controller explicitly reference
that single layout with the |
ActionController::Layout Classes Documentation |
Rails Project Directory Structure | Rails provides scripts for building Rails-based applications. These scripts create the appropriate directory structures that Rails assumes for tying together various pieces of a Rails application. | Entire Application | ||
Unit Testing File Names | The files for unit testing (model testing) are named relative to the
model name (<model_name>_test.rb |
Unit Tests | ||
Functional Testing file Names | The files for functional testing (controllers) are named relative to
the (usually plural) table name
(<table_name>_controller_test.rb |
Functional Tests | ||
Fixture File Names | Fixture configuration files are YAML format (.yml ) and
their names match the (usually plural) table names
(<table_name>.yml ). |
Unit Testing |
This page's URI: http://www.geocities.com/dustinmarx/SW/rails/conventions.html