跳至內容 跳至搜尋

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')

如果雜湊條件中可為 Null 欄位有非 Null 條件,則不會傳回具有 Null 值的可為 Null 欄位的記錄。

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