Learn Rails5.2-- ActiveRecord, Query查询语法。

Posted chentianwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Learn Rails5.2-- ActiveRecord, Query查询语法。相关的知识,希望对你有一定的参考价值。

 

rails generate model photo title:string album:references

这会产生一个album_id列,当建立belongs_to关联时,需要用到。

references算时一种结构,会产生integer.

 

For integer, string, text and binary fields, an integer in curly braces will be set as the limit:  `rails generate model user pseudo:string{30}`
For decimal, two integers separated by a comma in curly braces will be used for precision and scale: rails generate model product price:decimal{10,2}
You can add a `:uniq` or `:index` suffix for unique  
        `rails generate model user pseudo:string:uniq`
        `rails generate model user pseudo:string:index`
You can combine any single curly brace option with the index options:
        `rails generate model user username:string{30}:uniq`

 

Database Configuration 

config/database.yml中可以看到默认的数据库配置。

 

development:
  <<: *default
  database: db/development.sqlite3

 

使用sqlite3+路径, 进入控制台。

$ sqlite3 db/development.sqlite3
SQLite version 3.14.0 2016-07-26 15:17:14
Enter ".help" for usage hints.
sqlite> .tables #显示所有表格
ar_internal_metadata  products
countries             schema_migrations
sqlite> .schema countries #打印创建表格的命令。
CREATE TABLE "countries" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "population" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);

 


 

在rails console建立一条记录,可以通过底层数据库sqlite3查看: 

$ sqlite3 db/development.sqlite3

> select * from countries;

1|Cermany|81831000|2018-06-15 07:39:00.370425|2018-06-15 

表示成功。

 


 

一次创建多条数据需要使用一个数组的hashes。

Country.create([{name: "China"}, {name:"France"}]) 

 

Country.all返回得到一个数组的Country。它是一个ActiveRecord::Relation scope object

因此,Country.all可以使用each方法。

 


 

rails db:drop 删除数据库。rails db:create, rails db:migrate三剑客。

rails db:seed建立种子文件。

 


 

 

find , where 

 

使用查询方法的使用,如果是多条,必然返回一个Array。

Country.find(1)返回的是一个单独的object,他的类是Country

Country.find([1])返回的则是一个集合[],他的类是 Array

 

这里用到了between..and.. 

> Album.where(release_year:1960..1966).count
   (0.2ms)  SELECT COUNT(*) FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ?  [["release_year", 1960], ["release_year", 1966]]
=> 5

 

这里使用了and连接了2个between..and.. 

> Album.where(release_year:1960..1966, id: 1..5).count
   (0.2ms)  SELECT COUNT(*) FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ? AND "albums"."id" BETWEEN ? AND ?  [["release_year", 1960], ["release_year", 1966], ["id", 1], ["id", 5]]
=> 4

 

这里使用了in, 用于精确的指定查询条件

> Album.where(release_year:[1966, 1968]).count
   (0.1ms)  SELECT COUNT(*) FROM "albums" WHERE "albums"."release_year" IN (?, ?)  [["release_year", 1966], ["release_year", 1968]]
=> 4

 

这里order by 和 ASC, limit

> Album.where(release_year:[1966, 1968]).first
  Album Load (0.2ms)  SELECT  "albums".* FROM "albums" WHERE "albums"."release_year" IN (?, ?) ORDER BY "albums"."id" ASC LIMIT ?  [["release_year", 1966], ["release_year", 1968], ["LIMIT", 1]]
=> #<Album:0x00007ffd8cb9b388...>

 

所有查询语法,条件都可以使用?, (?, ?)代替,在语法最后使用Array,按顺序列出具体条件。 

 

 


 

 

not ,or 

 

这里使用了!= 

> Album.where.not(release_year: 1968).count
   (0.1ms)  SELECT COUNT(*) FROM "albums" WHERE "albums"."release_year" != ?  [["release_year", 1968]]
=> 9

 

这里使用了数据库的or,另外rails语法,or()方法连接的应该是同一个数据库

> Album.where(release_year:1967).or(Album.where(name: "The Beathles")).count
   (0.2ms)  SELECT COUNT(*) FROM "albums" WHERE ("albums"."release_year" = ? OR "albums"."name" = ?)  [["release_year", 1967], ["name", "The Beathles"]]

 

??和 Album.where(release_year:1960..1966, id: 1..5).count   的区别

 

 


 

自定义SQL,使用find_by_sql()方法。

 


 

 

模糊查找, >= , <=

 

> Album.where("name like ?", "%on%").count

 

   (0.1ms)  SELECT COUNT(*) FROM "albums" WHERE (name like ‘%on%‘)
=> 5

 

> Album.where(‘release_year > ?‘, 1964).count
   (0.2ms)  SELECT COUNT(*) FROM "albums" WHERE (release_year > 1964)
=> 10

 

使用了AND 

> Album.where("name like ? AND release_year > ?", ‘%on%‘, 1970).count
   (0.1ms)  SELECT COUNT(*) FROM "albums" WHERE (name like ‘%on%‘ AND release_year > 1970)
=> 3

 

使用了local variable在语法内部用了#{}插入符号 

> Album.where("name like ?", "%#{search_string}%").count
   (0.2ms)  SELECT COUNT(*) FROM "albums" WHERE (name like ‘%ing%‘)
=> 2

 

 


 

 链式查询:limit

这里用到limit方法限制查询记录数量。 

Ablum.where(release_year: 1965..1968).order(:release_year).limit(3)

 =>SELECT  "albums".* FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ? ORDER BY "albums"."release_year" ASC LIMIT ?  [["release_year", 1965], ["release_year", 1968], ["LIMIT", 3]]

 


 

 

自动的优化查询:

 

因为order对sum来说是无关紧要的查询条件。所以SQL没有使用order查询。

> Album.where(release_year: 1970..1979).order(:name).sum(:release_year)

 

 =>  (42.5ms)  SELECT SUM("albums"."release_year") FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ?  [["release_year", 1970], ["release_year", 1979]]
=> 5922

 

 


 

 

reverse_order和 order

 

reverse_order反转查询顺序。

 Album.where(release_year: 1960..1969).order(:name).reverse_order

 


 

Pluck()方法 

从检索的记录中挑出需要的字段。

这里返回一个Array,包含了所以符合查询条件的记录的名字。

可以挑出多个字段,返回嵌套数组。

 

> Album.where(release_year: 1960..1969).pluck(:name)
    SELECT "albums"."name" FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ?  [["release_year", 1960], ["release_year", 1969]]
=> ["Sgt. Pepper‘s Lonely Hearts Club Band",
 "Pet Sounds",
 "Revolver",
 "Highway 61 Revisited",
 "Rubber Soul",
 "Blonde on Blonde",
 "The Beatles"]

 

??Album.pluck(:id)等同于Album.ids 

 

 


 

 

 Select()方法 类似pluck()

返回的是一个ActiveRecord::Relation。

 

> Album.where(release_year: 1960..1969).select(:name)
   SELECT "albums"."name" FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ?  [["release_year", 1960], ["release_year", 1969]]
=> [#<Album:0x00007ffd8bb808e0 id: nil, name: "Sgt. Pepper‘s Lonely Hearts Club Band">,
 #<Album:0x00007ffd8bb805c0 id: nil, name: "Pet Sounds">,
 #<Album:0x00007ffd8bb80480 id: nil, name: "Revolver">,
 #<Album:0x00007ffd8bb80340 id: nil, name: "Highway 61 Revisited">,
 #<Album:0x00007ffd8bb80188 id: nil, name: "Rubber Soul">,
 #<Album:0x00007ffd8bb7bed0 id: nil, name: "Blonde on Blonde">,
 #<Album:0x00007ffd8bb7bbb0 id: nil, name: "The Beatles">]

 

 


 

 

 Calculations

 

average()方法。根据检索的条件,计算平均值,返回BigDecimal.

 

 > Album.where(release_year: 1960..1969).average(:release_year)
    SELECT AVG("albums"."release_year") FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ?  [["release_year", 1960], ["release_year", 1969]]
=> 0.196614285714286e4

 

maximum(), minimum(), sum(),用法一样。

> Album.maximum(:release_year)
   (0.2ms)  SELECT MAX("albums"."release_year") FROM "albums"
=> 1979


 

 


 

 

SQL EXPLAIN

 

大型的数据库,EXPLAIN是一个很好的debugging 方法。可以显示详细的信息。 

> Album.where(release_year: 1960..1969).explain
  SELECT "albums".* FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ?  [["release_year", 1960], ["release_year", 1969]]
=> EXPLAIN for: SELECT "albums".* FROM "albums" WHERE "albums"."release_year" BETWEEN ? AND ? [["release_year", 1960], ["release_year", 1969]]
2|0|0|SCAN TABLE albums

 

 批处理:batches

find_each,

find_in_batches: yields batches to 块,作为一个模型的数组。

见其他博客,或guide。 

 


 

 



以上是关于Learn Rails5.2-- ActiveRecord, Query查询语法。的主要内容,如果未能解决你的问题,请参考以下文章

Learn Rails5.2- Scaffolding and REST

Ruby on Rails Installation(Learn Rails5.2)

Learn Rails5.2- ActiveRecord: Migration , spring的使用(不兼容的解决办法)

Your Trial period has expired. To continue using ActiveReports 10, contact GrapeCity at ActiveRe

rails5.2新特性--ActiveStorage, 使用80percent/rails-template

带有 master.key 的 Rails 5.2 - Heroku 部署