`

Ruby代码【愉悦的编程体验】

 
阅读更多

rake db:create:all # Create all the local databases defined in config/database.yml

 

rake db:drop # Drops the database for the current RAILS_ENV

 

rake db:collation # Retrieves the collation for the current environment's database

 

rake db:charset # Retrieves the charset for the current environment's database

 

 

传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。 

 

 

count()统计查询对象个数

db.students.find({‘address.state’ : ‘CA’}).count();//效率较高

db.students.find({‘address.state’ : ‘CA’}).toArray().length;//效率很低

 

 


limit()对限制查询数据返回个数

db.colls.find().limit(10)

 

skip()跳过某些数据

db.colls.find().skip(10)

 

 程序才正常跑起来

一言蔽之 在has_many中:User#accounts.build 在has_one中:User#build_account

 

切记 new :只是在内存中新建一个对象,操作数据库要调用save方法。

create = new + 执行sql。

build:与new基本相同,多用于一对多情况下。还有一个不同请看使用示例 !:new!, create!, build!与new, create, build的区别是带!的方法会执行validate,如果验证失败会抛出导常。 save是实例方法,而create, build, new是模型类的类方法.

 

2)        find_by_(find_first_by)、find_last_by_、find_all_by_

 

 

 

==

 

redirect_to

5、render_to_string 渲染字符串(最直接的方式)

10、渲染文本   render :text => "OK"

11、渲染JSON    render :json => @product
Rails首先会在app/views/layouts目录下查找与controller同名的layout文件,
如果没有找到,则会使用app/views/layouts/application.html.erb
 
12.
16、使用跳转redirect_to
redirect_to给浏览器的响应是:告诉浏览器发起一个新的请求
17、redirect_to和render的区别
render渲染用户指定的模板作为响应
redirect_to会结束当前响应,并告诉浏览器请求一个新的url
 
 ==
旁路加载 : 不影响主线程,创建数据的时候就加载上了。。。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics