# scope ``` /model scope: cheap, -> { where("price > 50")} scope: adult, -> { where("age > 18")} ``` ``` /controller Product.cheap //找出價格低於50的商品 //等於Product.where("price > 50") ``` ``` Product.cheap.adult //找出價格低於50的成人商品 //可以串多個scope ``` * scope的效果類似於類別方法 `default_scope { order(id: :desc) }` * default_scope會強制所有經由此model的查詢都執行order(id: :desc) * 要關閉必須用`unscope(要取消的)`,如`comment.unscope(:order).order(新的排序)`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up