跳至内容 跳至搜索

WhereChain 对象充当查询的占位符,其中 where 没有参数。在这种情况下,where 可以链接以返回一个新的关系。

方法
A
M
N

实例公共方法

associated(*associations)

返回一个新的关系,其中包含连接和 where 子句,以识别关联的关系。

例如,与相关作者关联的帖子

Post.where.associated(:author)
# SELECT "posts".* FROM "posts"
# INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# WHERE "authors"."id" IS NOT NULL

此外,可以组合多个关系。这将返回与作者和所有评论都关联的帖子

Post.where.associated(:author, :comments)
# SELECT "posts".* FROM "posts"
# INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
# WHERE "authors"."id" IS NOT NULL AND "comments"."id" IS NOT NULL

您可以在范围内定义连接类型,并且 associated 默认情况下不会使用 'JOIN`。

 Post.left_joins(:author).where.associated(:author)
 # SELECT "posts".* FROM "posts"
 # LEFT OUTER JOIN "authors" "authors"."id" = "posts"."author_id"
 # WHERE "authors"."id" IS NOT NULL

 Post.left_joins(:comments).where.associated(:author)
 # SELECT "posts".* FROM "posts"
 # INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
 # LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"
#  WHERE "author"."id" IS NOT NULL
# File activerecord/lib/active_record/relation/query_methods.rb, line 88
def associated(*associations)
  associations.each do |association|
    reflection = scope_association_reflection(association)
    unless @scope.joins_values.include?(reflection.name) || @scope.left_outer_joins_values.include?(reflection.name)
      @scope.joins!(association)
    end

    association_conditions = Array(reflection.association_primary_key).index_with(nil)
    if reflection.options[:class_name]
      self.not(association => association_conditions)
    else
      self.not(reflection.table_name => association_conditions)
    end
  end

  @scope
end

missing(*associations)

返回一个新的关系,其中包含左外连接和 where 子句,以识别丢失的关系。

例如,缺少相关作者的帖子

Post.where.missing(:author)
# SELECT "posts".* FROM "posts"
# LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# WHERE "authors"."id" IS NULL

此外,可以组合多个关系。这将返回既缺少作者又缺少任何评论的帖子

Post.where.missing(:author, :comments)
# SELECT "posts".* FROM "posts"
# LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"
# WHERE "authors"."id" IS NULL AND "comments"."id" IS NULL
# File activerecord/lib/active_record/relation/query_methods.rb, line 124
def missing(*associations)
  associations.each do |association|
    reflection = scope_association_reflection(association)
    @scope.left_outer_joins!(association)
    association_conditions = Array(reflection.association_primary_key).index_with(nil)
    if reflection.options[:class_name]
      @scope.where!(association => association_conditions)
    else
      @scope.where!(reflection.table_name => association_conditions)
    end
  end

  @scope
end

not(opts, *rest)

返回一个新的关系,根据参数中的条件表达 WHERE + NOT 条件。

not 接受条件作为字符串、数组或哈希。有关每种格式的更多详细信息,请参见 QueryMethods#where

User.where.not("name = 'Jon'")
# SELECT * FROM users WHERE NOT (name = 'Jon')

User.where.not(["name = ?", "Jon"])
# SELECT * FROM users WHERE NOT (name = 'Jon')

User.where.not(name: "Jon")
# SELECT * FROM users WHERE name != 'Jon'

User.where.not(name: nil)
# SELECT * FROM users WHERE name IS NOT NULL

User.where.not(name: %w(Ko1 Nobu))
# SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu')

User.where.not(name: "Jon", role: "admin")
# SELECT * FROM users WHERE NOT (name = 'Jon' AND role = 'admin')

如果在哈希条件中,可为空的列上存在非空条件,则不会返回在可为空的列上具有空值的记录。

User.create!(nullable_country: nil)
User.where.not(nullable_country: "UK")
# SELECT * FROM users WHERE NOT (nullable_country = 'UK')
# => []
# File activerecord/lib/active_record/relation/query_methods.rb, line 49
def not(opts, *rest)
  where_clause = @scope.send(:build_where_clause, opts, rest)

  @scope.where_clause += where_clause.invert

  @scope
end