Saturday, November 29, 2008

Rails conventions

Models (and therefore the underlying classes they create) are capitalized and singular. Each creates a table that is lower-cased and plural.
 ./script/generate scaffold Student name:string
rake db:migrate
Looking at db/schema.rb shows:
ActiveRecord::Schema.define(:version => 20081129213933) do

create_table "students", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"

The views and controllers are plural: app/views/students and app/controllers/students_controller.rb, but the model is singular: app/models/student.rb. Remember there is a hidden "id" field in each row of each table so don't create your own "student_id" field.

And from
Model Naming Convention

Table: orders
Class: Order
File: /app/models/order.rb
Primary Key: id
Foreign Key: customer_id
Link Tables: items_orders

Controller Naming Convention

Class: OrdersController
File: /app/controllers/orders_controller.rb
Layout: /app/layouts/orders.html.erb

View Naming Convention

Helper: /app/helpers/orders_helper.rb
Helper Module: OrdersHelper
Views: /app/views/orders/… (list.html.erb for example)

No comments: