Class: ArticleRelation

Inherits:
Cybertrain::Relation show all
Defined in:
docs/api/stubs/generated_app.rb

Overview

The relation spin run gen writes next to each model (ArticleRelation for Article): the chainable query methods and the methods that load records. Start one with Article.all or a shortcut such as Article.where. The chain methods change this relation and return it.

Examples:

Article.where(title: "Hello").order("created_at DESC").limit(5).to_a
Comment.where(article_id: @article.id).count

Instance Method Summary collapse

Methods inherited from Cybertrain::Relation

#count, #delete_all, #exists?, #to_sql

Instance Method Details

#each {|article| ... } ⇒ Object

to_a.each.

Yield Parameters:



299
# File 'docs/api/stubs/generated_app.rb', line 299

def each(&blk) = to_a.each(&blk)

#find(id) ⇒ Article

The record with this id within the relation.

Examples:

Comment.where(article_id: @article.id).find(params[:id])

Parameters:

  • id (Integer, String)

Returns:

Raises:



328
# File 'docs/api/stubs/generated_app.rb', line 328

def find(id); end

#find_by(conditions) ⇒ Article?

where(conditions).first. Like every chain method, it narrows this relation itself.

Parameters:

  • conditions (Hash{Symbol => Object})

Returns:



319
# File 'docs/api/stubs/generated_app.rb', line 319

def find_by(conditions) = where(conditions).first

#first ⇒ Article?

The first record by the relation’s order (by id when it has none), from the #offset on.

Returns:



305
# File 'docs/api/stubs/generated_app.rb', line 305

def first; end

#last ⇒ Article?

Without a limit or offset, the first record of the reversed order (the highest id when there is no order); with them, the last record of that window.

Returns:



312
# File 'docs/api/stubs/generated_app.rb', line 312

def last; end

#limit(n) ⇒ self

Parameters:

  • n (Integer) —

    at most this many rows; 0 returns none

Returns:

  • (self)


281
# File 'docs/api/stubs/generated_app.rb', line 281

def limit(n) = (set_limit(n); self)

#offset(n) ⇒ self

Parameters:

  • n (Integer) —

    skip this many rows (works without a #limit)

Returns:

  • (self)


286
# File 'docs/api/stubs/generated_app.rb', line 286

def offset(n) = (set_offset(n); self)

#order(order) ⇒ self

Sets the order, replacing any earlier one. Takes only comma-separated column [ASC|DESC] terms (a Symbol is a column name; nil or "" removes the order). Request data must go through an allowlist first, since even a well-formed term can name a column the client must not sort by.

Examples:

Article.order("created_at DESC, id")
SORTS = { "title" => "title", "newest" => "created_at DESC" }
Article.order(SORTS.fetch(params[:sort].to_s, "id"))

Parameters:

  • order (String, Symbol, nil)

Returns:

  • (self)

Raises:

  • (ArgumentError) —

    for anything else (lower(title), articles.title): use #order_sql for raw SQL



266
# File 'docs/api/stubs/generated_app.rb', line 266

def order(order) = (set_order(order); self)

#order_sql(sql) ⇒ self

Sets a raw ORDER BY fragment, replacing any earlier order. Never pass request data. #last reverses it by flipping ASC/DESC around commas, which can mangle text such as COALESCE(a, b).

Examples:

Article.order_sql("lower(title)")

Parameters:

  • sql (String)

Returns:

  • (self)


276
# File 'docs/api/stubs/generated_app.rb', line 276

def order_sql(sql) = (add_order_sql(sql); self)

#size ⇒ Integer

Same as count.

Returns:

  • (Integer)


333
# File 'docs/api/stubs/generated_app.rb', line 333

def size = count

#to_a ⇒ Array<Article>

Runs the query. Templates iterate an Array, so controllers usually end a query with to_a.

Examples:

@articles = Article.all.to_a

Returns:



294
# File 'docs/api/stubs/generated_app.rb', line 294

def to_a; end

#where(conditions) ⇒ self

Adds equality conditions, AND-ed with any already there. Keys are SQL column names; values are bound, never interpolated.

Value SQL
nil `col` IS NULL
an Array `col` IN (?, ?) (an empty Array matches nothing)
a Time `col` = ? with the UTC ISO 8601 text
true / false `col` = ? with 1 / 0
anything else `col` = ?

No ranges, no not, no or: use #where_sql for those.

Examples:

Article.where(title: ["Hello", "Bye"]).where(body: nil)

Parameters:

  • conditions (Hash{Symbol => Object})

Returns:

  • (self)


239
# File 'docs/api/stubs/generated_app.rb', line 239

def where(conditions) = (add_where(conditions); self)

#where_sql(sql, binds = []) ⇒ self

Adds a raw SQL condition with ? placeholders, AND-ed with the others. The fragment is used as written: never build it from request data; pass values as binds.

Examples:

Article.all.where_sql("created_at >= ?", [Time.now - 86_400])

Parameters:

  • sql (String)
  • binds (Array) (defaults to: []) —

    one value per ?

Returns:

  • (self)


250
# File 'docs/api/stubs/generated_app.rb', line 250

def where_sql(sql, binds = []) = (add_where_sql(sql, binds); self)