11月26日 用seed,预加载种子文件; Case 条件语句。网址的参数如何传递,; Query--自定义scopes
正确✅:
for i in 1..10 do
然后在terminal输入rake db:seed
case语法:
Active Record Query Interface
路径传递参数的用法:网址里面带参数order(也是query)
job_path(里面可以传id参数来调用controller里面的show),
jobs_path(里面也可以传设定的参数,如👇的,调用的是controller里面的index action)
<li><%= link_to("按照薪资下限排序", jobs_path(:order => "by_lower_bound") )%></li>
参数传递给controller后执行相应的action内的查询结果,最后给view视图并生成 /jobs?order=by_lower_bound 这样的网址
Query--14 Scopes
Scoping allows you to specify commonly-used queries which can be referenced as method calls on the association objects or models.
作用域 允许 你 定义 经常用到的 查询语句,可以作为method召唤关联的对象或模型。
query语句的大量method如where,joins,includes都可以和scopes一起使用。
Scope methods return 一个relation object 允许其他method调用它,因此scope可以被别的scope调用
class Article < ApplicationRecord scope :published, -> { where(published: true) }end等同于
class Article < ApplicationRecord def self.published where(published: true) endend |
可以使用复杂的嵌套scope.因为scope可以被别的scope调用。
class Article < ApplicationRecord scope :published, -> { where(published: true) } scope :published_and_commented, -> { published.where("comments_count > 0") }end |
14.1 Passing in arguments
Your scope can take arguments:
pasting
14.2 Using conditionals
Your scope can utilize conditionals;
14.4 Merging of scopes
比较复杂的混合查询。
14.5 Removing All Scoping
unscoped method,配合默认的scope
原文:英语
http://guides.rubyonrails.org/active_record_querying.html#passing-in-arguments
浙公网安备 33010602011771号