class ActiveRecord::QueryMethods::WhereChain

In this case, where can be chained to return a new relation.
WhereChain objects act as placeholder for queries in which where does not have any parameter.

def associated(*associations)

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

associated to both an author and any comments:
Additionally, multiple relations can be combined. This will return posts

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

For example, posts that are associated to a related author:

associated relations.
Returns a new relation with joins and where clause to identify
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

def initialize(scope) # :nodoc:

:nodoc:
def initialize(scope) # :nodoc:
  @scope = scope
end

def missing(*associations)

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

that are missing both an author and any comments:
Additionally, multiple relations can be combined. This will return posts

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

For example, posts that are missing a related author:

missing relations.
Returns a new relation with left outer joins and where clause to identify
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

def not(opts, *rest)

# => []
# SELECT * FROM users WHERE NOT (nullable_country = 'UK')
User.where.not(nullable_country: "UK")
User.create!(nullable_country: nil)
nil values on the nullable column won't be returned.
If there is a non-nil condition on a nullable column in the hash condition, the records that have

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

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

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

# SELECT * FROM users WHERE name != 'Jon'
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'")

more details on each format.
#not accepts conditions as a string, array, or hash. See QueryMethods#where for

the conditions in the arguments.
Returns a new relation expressing WHERE + NOT condition according to
def not(opts, *rest)
  where_clause = @scope.send(:build_where_clause, opts, rest)
  @scope.where_clause += where_clause.invert
  @scope
end

def scope_association_reflection(association)

def scope_association_reflection(association)
  reflection = @scope.klass._reflect_on_association(association)
  unless reflection
    raise ArgumentError.new("An association named `:#{association}` does not exist on the model `#{@scope.name}`.")
  end
  reflection
end