Rails where用法

rails的都用过where方法,总结了一些基本的,欢迎提供更多的内容。

where方法用来指定限制获取记录的条件,用于 sql语句的where子句。条件可使用字符串、数组或 Hash 指定。

1.String纯字符串条件
>> Applicant.where("name = '夏女士'")
=> #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士');
2.Array数组

对于号,Active Record会将先处理第一个元素中的条件,然后使用后续元素替换第一个元素中的问号?,问号个数与后续元素个数相同。

>> Applicant.where(["name = ? and sex = ?", "夏女士", 1]) 或
>> Applicant.where("name = ? and sex = ?", "夏女士", 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士' and sex = 1)

除了使用问号占位之外,在数组条件中还可使用键值对 Hash 形式的占位符

>> Applicant.where(["name = :name and sex = :sex", { name: "夏女士", sex:  1}]) 或
>> Applicant.where("name = :name and sex = :sex", { name: "夏女士", sex: 1 })
=>  #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士' and sex = 1)

还能这样:

>> Applicant.where(["name = '%s' and sex = '%s'", "夏女士", 1]) 或
>> Applicant.where("name = '%s' and sex = %s", "夏女士", 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE (name = '夏女士' and sex = 1)
3.Hash

下面最外层的{}符号是可以去掉的

>> Applicant.where({ name: "夏女士", sex: 1 })  可以写成
>> Applicant.where(name: "夏女士", sex: 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`name` = '夏女士' AND `applicants`.`sex` = 1

子集查询

>> Applicant.where(name: ["夏女士", "张女士"])
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`name` IN ('夏女士', '张女士')

查询name夏女士张女士的记录,使用的是in子句查询记录

范围查询

>> Applicant.where(id: 1..5)
=> #SELECT `applicants`.* FROM `applicants` WHERE (`applicants`.`id` BETWEEN 1 AND 5)

查询id 在1到5之间的值,用的sql between 查询,区别于[1,2,3,4,5]。更常用于时间段查询。

字段的名字还可使用字符串表示:

>> Applicant.where('sex' => 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`sex` = 1
4.Joins关联

下面是两个model的关系

class Applicant < ActiveRecord::Base
  has_many :companies
end
class Company < ActiveRecord::Base
  belongs_to :applicant
   #
end

通过关联查询companies表中type_id为1的所有applicants记录:

Applicant.joins(:companies).where("companies.type_id = ?", 1)
#SELECT `applicants`.* FROM `applicants` INNER JOIN `companies` ON `companies`.`applicant_id` = `applicants`.`id` WHERE (companies.type_id = 1)

或者这样:

>> Applicant.joins(:companies).where(companies: { type_id: 1 })
=>  #SELECT `applicants`.* FROM `applicants` INNER JOIN `companies` ON `companies`.`applicant_id` = `applicants`.`id` WHERE `companies`.`type_id` = 1

下面的方法是不行的:

>> Applicant.joins(:companies).where(type_id: 1)
=> #Mysql2::Error: Unknown column 'applicants.type_id' in 'where clause'
5.where.not用法

sql not查询可用where.not方法构建。

>> Applicant.where.not(sex: 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE (`applicants`.`sex` != 1)

如果还想用where查询其他字段:

>> Applicant.where(name: '夏女士').where.not(sex: 1)
=> #SELECT `applicants`.* FROM `applicants` WHERE `applicants`.`name` = '夏女士' AND (`applicants`.`sex` != 1)

关联查询companies表中type_id不为空的记录:

>> Applicant.includes(:companies).where.not(companies: {type_id: nil})
=> #SELECT `applicants`.* FROM `applicants` LEFT OUTER JOIN `companies` ON `companies`.`applicant_id` = `applicants`.`id` WHERE (`companies`.`type_id` IS NOT NULL)

欢迎指正、补充!

你可能感兴趣的:(Rails where用法)