Notes on Ruby-on-Rails
Rails Notes
bundle exec rake middleware
- Rack: a ruby web server interface
rails about
rails new -B
skip bundle install- Non-id routes of singular
...routes.draw
resource: config # sigular form
#### NON-ID routes generated, rather than config/:id/edit
resource users, shallow: true
resource: articles # routes WITHOUT prefix "users"
- AppCtrller:
protect_from_forgery: with :exception
#一般防CSRFprotect_from_forgery: with :null_session
for API
flash[:note]
在重定向时用;flash.now[:alert]
在直接渲染时使用;- flash只能存活到下一个请求到来;
rails c --sandbox
清空DB changes after console exitrails db:migrate:status
- config/app….rb: schema_format=
:sql
或者:ruby
MyModel.joins(:articles)
join on recorde [DB]User.where(...).or(User.where(...))
OR- scope限定范围,不能使用default scope,无法被覆盖;
- 大纪录可以用
find_each
或者in_batches
处理。 - rails console 末尾加上分号
;
可以强制求值; - Rspec:
let(:article) {Article.new(...) }
- 引入局部变量;
- 是lazy loading
- Rails files:
.ruby-version
.editorconfig
Order < ApplicationRecord
belongs_to :customer
delegate :name, :address, to: :customer
end
#
Order.new.address # simplify nested call
- ActiveSupport:
obj.ry(:method)
若obj为nil, 返回nil; 否则执行method
- 可在appController detect browser时,设定
request.variant = :smart
对应智能手机设备 - 可以
User.includes(:article)
,而不是article.comments.each do |c| c.user.name
.这样可以避免N+1查询问题。