跳过内容 跳过搜索

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
# File activerecord/lib/active_record/relation/query_methods.rb, line 75
def associated(*associations)
  associations.each do |association|
    reflection = scope_association_reflection(association)
    @scope.joins!(association)
    if reflection.options[:class_name]
      self.not(association => { reflection.association_primary_key => nil })
    else
      self.not(reflection.table_name => { reflection.association_primary_key => nil })
    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 107
def missing(*associations)
  associations.each do |association|
    reflection = scope_association_reflection(association)
    @scope.left_outer_joins!(association)
    if reflection.options[:class_name]
      @scope.where!(association => { reflection.association_primary_key => nil })
    else
      @scope.where!(reflection.table_name => { reflection.association_primary_key => nil })
    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