ruby-rails笔记

1.rails new blog

2.cd blog

3.rails server

4.rails generate controller Welcome index

5.rails routes

6.rails generate model Article title:string text:text

7.rails db:migrate

8.rails generate controller Comments

9.外键

Foreign keys - These fields should be named following the pattern singularized_table_name_id (e.g., item_id, order_id). These are the fields that Active Record will look for when you create associations between your models.

10.数据库的操作

Creating Active Record Models It is very easy to create Active Record models. All you have to do is to subclass the ApplicationRecord class and you’re good to go:

class Product < ApplicationRecord
end

This will create a Product model, mapped to a products table at the database. By doing this you’ll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. Suppose that the products table was created using an SQL statement like:

CREATE TABLE products (
   id int(11) NOT NULL auto_increment,
   name varchar(255),
   PRIMARY KEY  (id)
);

Following the table schema above, you would be able to write code like the following:

p = Product.new
p.name = "Some Book"
puts p.name # "Some Book"

1.增

 test = Test.create(name: "David")
test = Test.new
test.name = '222'
test.save

2.读

users = User.all
user = User.first
david = User.find_by(name: 'David')
users = User.where(name: 'David', 
occupation: 'Code Artist').order
(created_at: :desc)

3.更新


user = User.find_by(name: 'David')
user.name = 'Dave'
user.save
user = User.find_by(name: 'David')
user.update(name: 'Dave')

4.删除

user = User.find_by(name: 'David')
user.destroy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值