Movatterモバイル変換


[0]ホーム

URL:


Skip to ContentSkip to Search
Ruby on Rails 8.1.1

Class ActiveRecord::QueryMethods::WhereChain<Object

v8.1.1

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

Methods
A
M
N

Instance Public methods

associated(*associations)Link

Returns a new relation with joins and where clause to identify associated relations.

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

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

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

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

You can define join type in the scope andassociated will not use ‘JOIN` by default.

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 NULLPost.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

Source:show |on GitHub

# File activerecord/lib/active_record/relation/query_methods.rb, line 88defassociated(*associations)associations.eachdo|association|reflection =scope_association_reflection(association)unless@scope.joins_values.include?(reflection.name)||@scope.left_outer_joins_values.include?(reflection.name)@scope.joins!(association)endassociation_conditions =Array(reflection.association_primary_key).index_with(nil)ifreflection.options[:class_name]self.not(association=>association_conditions)elseself.not(reflection.table_name=>association_conditions)endend@scopeend

missing(*associations)Link

Returns a new relation with left outer joins and where clause to identify missing relations.

For example, posts that are missing a related author:

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

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

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

Source:show |on GitHub

# File activerecord/lib/active_record/relation/query_methods.rb, line 124defmissing(*associations)associations.eachdo|association|reflection =scope_association_reflection(association)@scope.left_outer_joins!(association)association_conditions =Array(reflection.association_primary_key).index_with(nil)ifreflection.options[:class_name]@scope.where!(association=>association_conditions)else@scope.where!(reflection.table_name=>association_conditions)endend@scopeend

not(opts, *rest)Link

Returns a new relation expressing WHERE + NOT condition according to the conditions in the arguments.

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

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

If there is a non-nil condition on a nullable column in the hash condition, the records that have nil values on the nullable column won’t be returned.

User.create!(nullable_country:nil)User.where.not(nullable_country:"UK")# SELECT * FROM users WHERE NOT (nullable_country = 'UK')# => []

Source:show |on GitHub

# File activerecord/lib/active_record/relation/query_methods.rb, line 49defnot(opts,*rest)where_clause =@scope.send(:build_where_clause,opts,rest)@scope.where_clause+=where_clause.invert@scopeend

[8]ページ先頭

©2009-2025 Movatter.jp