Show generated SQL as comment wihout => in all examples [ci-skip]

If we show the generated SQL in a Ruby code block, some examples used:
* a  hash rocket `=>` (about  29 times)
* no hash rocket      (about 124 times)

The hash rocket is shown in IRB to show the return value of the last
statement. As SQL is never returned this way, and for consistency the
hash rocket has been removed.
This commit is contained in:
Petrik 2023-02-06 18:22:33 +01:00
parent 52a525b3ae
commit 32aaeba0ae
6 changed files with 38 additions and 36 deletions

@ -1045,45 +1045,45 @@ def association_instance_set(name, association)
# Indexes are appended for any more successive uses of the table name.
#
# Post.joins(:comments)
# # => SELECT ... FROM posts INNER JOIN comments ON ...
# # SELECT ... FROM posts INNER JOIN comments ON ...
# Post.joins(:special_comments) # STI
# # => SELECT ... FROM posts INNER JOIN comments ON ... AND comments.type = 'SpecialComment'
# # SELECT ... FROM posts INNER JOIN comments ON ... AND comments.type = 'SpecialComment'
# Post.joins(:comments, :special_comments) # special_comments is the reflection name, posts is the parent table name
# # => SELECT ... FROM posts INNER JOIN comments ON ... INNER JOIN comments special_comments_posts
# # SELECT ... FROM posts INNER JOIN comments ON ... INNER JOIN comments special_comments_posts
#
# Acts as tree example:
#
# TreeMixin.joins(:children)
# # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
# # SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
# TreeMixin.joins(children: :parent)
# # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
# INNER JOIN parents_mixins ...
# # SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
# # INNER JOIN parents_mixins ...
# TreeMixin.joins(children: {parent: :children})
# # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
# INNER JOIN parents_mixins ...
# INNER JOIN mixins childrens_mixins_2
# # SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ...
# # INNER JOIN parents_mixins ...
# # INNER JOIN mixins childrens_mixins_2
#
# Has and Belongs to Many join tables use the same idea, but add a <tt>_join</tt> suffix:
#
# Post.joins(:categories)
# # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
# # SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
# Post.joins(categories: :posts)
# # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
# INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
# # SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
# # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
# Post.joins(categories: {posts: :categories})
# # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
# INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
# INNER JOIN categories_posts categories_posts_join INNER JOIN categories categories_posts_2
# # SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ...
# # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories
# # INNER JOIN categories_posts categories_posts_join INNER JOIN categories categories_posts_2
#
# If you wish to specify your own custom joins using ActiveRecord::QueryMethods#joins method, those table
# names will take precedence over the eager associations:
#
# Post.joins(:comments).joins("inner join comments ...")
# # => SELECT ... FROM posts INNER JOIN comments_posts ON ... INNER JOIN comments ...
# # SELECT ... FROM posts INNER JOIN comments_posts ON ... INNER JOIN comments ...
# Post.joins(:comments, :special_comments).joins("inner join comments ...")
# # => SELECT ... FROM posts INNER JOIN comments comments_posts ON ...
# INNER JOIN comments special_comments_posts ...
# INNER JOIN comments ...
# # SELECT ... FROM posts INNER JOIN comments comments_posts ON ...
# # INNER JOIN comments special_comments_posts ...
# # INNER JOIN comments ...
#
# Table aliases are automatically truncated according to the maximum length of table identifiers
# according to the specific database.

@ -194,10 +194,10 @@ module ClassMethods
# end
#
# Product.where(price_in_bitcoins: Money.new(5, "USD"))
# # => SELECT * FROM products WHERE price_in_bitcoins = 0.02230
# # SELECT * FROM products WHERE price_in_bitcoins = 0.02230
#
# Product.where(price_in_bitcoins: Money.new(5, "GBP"))
# # => SELECT * FROM products WHERE price_in_bitcoins = 0.03412
# # SELECT * FROM products WHERE price_in_bitcoins = 0.03412
#
# ==== Dirty Tracking
#

@ -463,28 +463,28 @@ def update!(id = :all, attributes)
# end
#
# developer = Developer.first
# SELECT "developers".* FROM "developers" ORDER BY "developers"."company_id" ASC, "developers"."id" ASC LIMIT 1
# # SELECT "developers".* FROM "developers" ORDER BY "developers"."company_id" ASC, "developers"."id" ASC LIMIT 1
# developer.inspect # => #<Developer id: 1, company_id: 1, ...>
#
# developer.update!(name: "Nikita")
# # => UPDATE "developers" SET "name" = 'Nikita' WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
# # UPDATE "developers" SET "name" = 'Nikita' WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
#
# It is possible to update attribute used in the query_by clause:
# developer.update!(company_id: 2)
# # => UPDATE "developers" SET "company_id" = 2 WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
# # UPDATE "developers" SET "company_id" = 2 WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
#
# developer.name = "Bob"
# developer.save!
# # => UPDATE "developers" SET "name" = 'Bob' WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
# # UPDATE "developers" SET "name" = 'Bob' WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
#
# developer.destroy!
# # => DELETE FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
# # DELETE FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
#
# developer.delete
# # => DELETE FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
# # DELETE FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1
#
# developer.reload
# # => SELECT "developers".* FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1 LIMIT 1
# # SELECT "developers".* FROM "developers" WHERE "developers"."company_id" = 1 AND "developers"."id" = 1 LIMIT 1
def query_constraints(*columns_list)
raise ArgumentError, "You must specify at least one column to be used in querying" if columns_list.empty?

@ -414,7 +414,7 @@ def cache_key_with_version
# Comment.where(post_id: 1).scoping do
# Comment.first
# end
# # => SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1 ORDER BY "comments"."id" ASC LIMIT 1
# # SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1 ORDER BY "comments"."id" ASC LIMIT 1
#
# If <tt>all_queries: true</tt> is passed, scoping will apply to all queries
# for the relation including +update+ and +delete+ on instances.
@ -729,7 +729,7 @@ def reset
# Returns sql statement for the relation.
#
# User.where(name: 'Oscar').to_sql
# # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
# # SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
def to_sql
@to_sql ||= if eager_loading?
apply_join_dependency do |relation, join_dependency|

@ -75,7 +75,8 @@ def default_scopes?(all_queries: false)
# default_scope { where(published: true) }
# end
#
# Article.all # => SELECT * FROM articles WHERE published = true
# Article.all
# # SELECT * FROM articles WHERE published = true
#
# The #default_scope is also applied while creating/building a record.
# It is not applied while updating or deleting a record.
@ -110,7 +111,8 @@ def default_scopes?(all_queries: false)
# default_scope { where(rating: 'G') }
# end
#
# Article.all # => SELECT * FROM articles WHERE published = true AND rating = 'G'
# Article.all
# # SELECT * FROM articles WHERE published = true AND rating = 'G'
#
# This is also the case with inheritance and module includes where the
# parent or module defines a #default_scope and the child or including

@ -2308,15 +2308,15 @@ You can also use `any?` and `many?` to check for existence on a model or relatio
```ruby
# via a model
Order.any?
# => SELECT 1 FROM orders LIMIT 1
# SELECT 1 FROM orders LIMIT 1
Order.many?
# => SELECT COUNT(*) FROM (SELECT 1 FROM orders LIMIT 2)
# SELECT COUNT(*) FROM (SELECT 1 FROM orders LIMIT 2)
# via a named scope
Order.shipped.any?
# => SELECT 1 FROM orders WHERE orders.status = 0 LIMIT 1
# SELECT 1 FROM orders WHERE orders.status = 0 LIMIT 1
Order.shipped.many?
# => SELECT COUNT(*) FROM (SELECT 1 FROM orders WHERE orders.status = 0 LIMIT 2)
# SELECT COUNT(*) FROM (SELECT 1 FROM orders WHERE orders.status = 0 LIMIT 2)
# via a relation
Book.where(out_of_print: true).any?