WhereChain 对象充当了 where 没有参数时的查询占位符。在这种情况下,where 可以被链式调用以返回一个新的 relation。
方法
- A
- M
- N
实例公共方法
associated(*associations) 链接
返回一个包含 join 和 where 子句的新 relation,用于标识关联的 relations。
例如,与相关作者关联的帖子
Post.where.associated(:author) # SELECT "posts".* FROM "posts" # INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" # WHERE "authors"."id" IS NOT NULL
此外,还可以组合多个 relations。这将返回同时与作者和任何评论关联的帖子
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
您可以在 scope 中定义 join 类型,并且 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
来源: 显示 | 在 GitHub 上
# 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 子句的新 relation,用于标识缺失的 relations。
例如,缺少相关作者的帖子
Post.where.missing(:author) # SELECT "posts".* FROM "posts" # LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id" # WHERE "authors"."id" IS NULL
此外,还可以组合多个 relations。这将返回同时缺少作者和任何评论的帖子
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
来源: 显示 | 在 GitHub 上
# 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 条件的新 relation。
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')
如果对可为空列的哈希条件中存在非 nil 条件,则不会返回可为空列值为 nil 的记录。
User.create!(nullable_country: nil) User.where.not(nullable_country: "UK") # SELECT * FROM users WHERE NOT (nullable_country = 'UK') # => []
来源: 显示 | 在 GitHub 上
# 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