Module: ActiveRecord::Refined::BlockSyntax

Includes:
AST::Arithmetics, AST::Predications
Defined in:
lib/active_record/refined.rb

Overview

What a symbol answers to inside a block. A symbol names a column there -- :age is "users"."age" -- and the methods below build a condition, an expression or an ordering from it. Every one of them is a refinement, so it exists inside a where, select, having, order, group, joins, update_all or upsert_all block and nowhere else.

The comparisons and the rest of the conditions are listed under AST::Predications, the arithmetic under AST::Arithmetics; a number or a string in a block takes as too, for a literal in a select list -- 0.as(:depth) -- and a number on the left of an operator builds the same expression a column on the left would.

Examples:

A column compared, aliased and ordered

Author.where { :age >= 18 }
Author.select { :name.as(:author) }
Author.order { :age.desc.nulls_last }

A column of another table, and a collation

Author.joins(:posts) { :posts[:author_id] == :authors[:id] }
Author.where { :name.collate(:nocase) == "alice" }

Instance Method Summary collapse

Methods included from AST::Arithmetics

#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~

Methods included from AST::Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_text, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #keys, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when

Instance Method Details

#[](column_name) ⇒ AST::Column

A column of another table: :posts[:author_id] is "posts"."author_id", for a join condition or a query over a join.

Examples:

Author.joins(:posts) { :posts[:author_id] == :authors[:id] }

Parameters:

  • column_name (Symbol)

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_record/refined.rb', line 71

refine Symbol do
  import_methods AST::Predications
  import_methods AST::Arithmetics

  def as(alias_name, quote: true)
    AST::As.new(self, alias_name, quote: quote)
  end

  def asc
    AST::Ordering.new(self, :asc)
  end

  def desc
    AST::Ordering.new(self, :desc)
  end

  def collate(name)
    AST::Collate.new(self, name)
  end

  def [](column_name)
    AST::Column.new(self, column_name)
  end
end

#as(alias_name, quote: true) ⇒ AST::As

The column under an alias: AS "name". The alias is quoted, so the name asked for is the name that comes back on every adapter; quote: false writes it bare, for a schema that wants the folding, and then it has to be a plain name.

Examples:

Author.select { :name.as(:author) }   # "authors"."name" AS "author"

Parameters:

  • alias_name (Symbol, String)
  • quote (Boolean) (defaults to: true)

Returns:



# File 'lib/active_record/refined.rb', line 29

#ascAST::Ordering

An ascending ordering, which takes nulls_first and nulls_last.

Examples:

Author.order { :country.asc.nulls_last }

Returns:



# File 'lib/active_record/refined.rb', line 40

#collate(name) ⇒ AST::Collate

The column under a collation, for a comparison or an ordering: "name" COLLATE nocase. The name is the database's own and not portable; PostgreSQL quotes it, the others take it bare and refuse one that is not a plain identifier.

Examples:

Author.where { :name.collate(:nocase) == "alice" }
Author.order { :name.collate(:"en-US-x-icu").asc }   # PostgreSQL

Parameters:

  • name (Symbol, String)

    the collation's name

Returns:



# File 'lib/active_record/refined.rb', line 52

#descAST::Ordering

A descending ordering, which takes nulls_first and nulls_last.

Examples:

Post.order { :likes.desc }

Returns:



# File 'lib/active_record/refined.rb', line 46