rails new rubyblog // create new project

config

database.yml: by default, use SQlit3

rails generate controller home index//home controller, index view

controller name usually are plural, model is single

models

Posts: the following attributes are automatically generated
id: integer (primary key)
create_date: timestamp
update_date: timestamp

======
title: string
category_id: integer (foreign key)

database association

belongs_to

has_many

post belongs_to category

category has_many post

rails console//enter into console mode
category = Category.new(attributes) //create a new instance of a class
//in database, this create an instance of a record
//in order to save the record
category.save
//2 -> 1 step
category = Category.save(attributes)

ps: rails 4 doesn't allow mass assignment. instead of attr_accessible :username, :email, :password, :password_confirmation, use Strong parameter (https://stackoverflow.com/questions/23437830/undefined-method-attr-accessible\

要不然无法assign values to record in db

results matching ""

    No results matching ""