Class: ArticleRelation
- Inherits:
-
Cybertrain::Relation
- Object
- Cybertrain::Relation
- ArticleRelation
- 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.
Instance Method Summary collapse
-
#each {|article| ... } ⇒ Object
to_a.each. -
#find(id) ⇒ Article
The record with this id within the relation.
-
#find_by(conditions) ⇒ Article?
where(conditions).first. -
#first ⇒ Article?
The first record by the relation’s order (by id when it has none), from the #offset on.
-
#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.
-
#limit(n) ⇒ self
-
#offset(n) ⇒ self
-
#order(order) ⇒ self
Sets the order, replacing any earlier one.
-
#order_sql(sql) ⇒ self
Sets a raw ORDER BY fragment, replacing any earlier order.
-
#size ⇒ Integer
Same as count.
-
#to_a ⇒ Array<Article>
Runs the query.
-
#where(conditions) ⇒ self
Adds equality conditions, AND-ed with any already there.
-
#where_sql(sql, binds = []) ⇒ self
Adds a raw SQL condition with
?placeholders, AND-ed with the others.
Methods inherited from Cybertrain::Relation
#count, #delete_all, #exists?, #to_sql
Instance Method Details
#each {|article| ... } ⇒ Object
to_a.each.
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.
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.
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.
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.
312 |
# File 'docs/api/stubs/generated_app.rb', line 312 def last; end |
#limit(n) ⇒ self
281 |
# File 'docs/api/stubs/generated_app.rb', line 281 def limit(n) = (set_limit(n); self) |
#offset(n) ⇒ 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.
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).
276 |
# File 'docs/api/stubs/generated_app.rb', line 276 def order_sql(sql) = (add_order_sql(sql); self) |
#size ⇒ Integer
Same as count.
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.
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.
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.
250 |
# File 'docs/api/stubs/generated_app.rb', line 250 def where_sql(sql, binds = []) = (add_where_sql(sql, binds); self) |