Common commands
Rails new New_app
cd New_app
rake db:create
Rails server
rails generate controller Blog action1 action2< C5/>rails Generate scaffold Product title:string Description:text
Rails Generate model Comment commenter:string body:text post:references
Rake db:migrate
rake db:rollback
Rails test:units
Rails Console
Conventions
There are a lot of conventions in rails, and it's these conventions that save us time, and let us clearly understand the structure of the project and the location of the file.
Table Name Conventions
The table name defaults to use the plural form of model, lowercase. For example: Model is sheep, the default table name is Sheeps, if we want to customize some other names, or based on an existing data table for development, can not modify the name of the datasheet, then we can use the following code to specify the name of the table.
Class Sheep < activerecord::base
Self.table_name = "Sheep"
End
Conventions for table primary keys
Default primary Key name ID, integer, self-increasing. The name in the datasheet is the ID, which is also accessed by the. ID in the model. If you want to specify a different name, you can do so by using the following code.
Class Legacybook < activerecord::base
Self.primary_ke = "ISBN"
end
After the above modification, the data table's primary key column name becomes ISBN, also through the model. ISBN to access. One exception, however, is to assign a value to a primary key or to use an ID to assign it.
Book = legacyboo.new
book.id = "1-214-985"
Book.title = "Programming in Ruby"
In addition to assigning a value to a primary key requires an ID, the specified column name is used at other times.
Model of the relationship
There are three kinds of table relationships:
One-to-one
One-to-many
Many-to-many
The declarations used in model are: Has_one, Has_many, Belongs_to, Has_and_belongs_to_many.
One-to-one
Class Order < ActiveRecord::Base
Has_one:invoice
End
class Invoice < ActiveRecord::Base
Belongs_to:order End
An order has an invoice header, a one-to-one relationship.
One thing is important: a table with a foreign key must have a belongs_to declaration.
One-to-many
Class Order < ActiveRecord::Base
Has_many:line_items
End
class LineItem < ActiveRecord::Base
Belongs_to:order End
An order will have a lot of item and a one-to-many relationship.
Many-to-many
Class Product < activerecord::base
has_and_belongs_to_many:categories
End
class Category < ActiveRecord::Base
has_and_belongs_to_many:p roducts
End
A product belongs to multiple catalogs, and a directory contains multiple products and many-to-many relationships. In addition to the Products table and Categories table, there will be an intermediate table categories_products (category_id, product_id) to store the relationship.
We can also define the relational tables ourselves, and we can store some other information, some information about the relationship. In fact, the relationship between many and many to separate, to become two a one-to-many relationship, so it is better to understand.
SOURCE http://virusswb.blog.51cto.com/115214/1016250