Class: Article
- Inherits:
-
Cybertrain::Model
- Object
- Cybertrain::Model
- Article
- Defined in:
- docs/api/stubs/generated_app.rb
Overview
What spin run gen writes into gen/models/article.rb for this table in
db/schema.rb (the overview has the rules for
every table):
s.create_table "articles" do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
The class is named after the singular of the table (articles → Article,
blog_posts → BlogPost), with one accessor per column, the finders below
and an association per foreign key. app/models/article.rb reopens it, with
no superclass, for validations,
callbacks and your own methods; never edit
the generated file.
Attributes. Each column has a reader and a writer (article.title,
article.title = "x") holding a Ruby value of the column’s type:
| Schema type | Ruby value |
|---|---|
string, text |
String ("" for a new record when null: false, else nil) |
integer, references |
Integer (0 when null: false, else nil) |
float |
Float |
boolean |
true / false |
datetime |
Time (UTC, to the second), nil until set |
date |
String ("2026-10-05"); there is no Date class |
new, update and #write_attribute cast what they are given ("42"
becomes 42 for an integer column, "1"/"true"/"on" becomes true); the
plain writer article.title = ... does not. A schema default: that is a
plain literal becomes the initial value of a new record.
A column named like a model method or a Ruby keyword (errors, hash,
class) gets a reader named <column>_column, with a note in the
generated file; queries keep using the SQL name.
Associations come from foreign keys only (t.references :article in a
migration, or add_reference): the table with the key gets a belongs_to
reader (Comment#article), the table it points to a has_many reader
(#comments). There is no has_many/belongs_to to declare, and no
has_one, :through, dependent: or includes.
Instance Attribute Summary collapse
-
#body ⇒ String?
The
bodycolumn (t.text "body"). -
#created_at ⇒ Time?
Set by the first save when nil.
-
#title ⇒ String?
The
titlecolumn (t.string "title"). -
#updated_at ⇒ Time?
Set by every save.
Attributes inherited from Cybertrain::Model
Class methods (finders) collapse
-
.all ⇒ ArticleRelation
A relation over the whole table; every query starts here or at one of the shortcuts below.
-
.column_names ⇒ Array<String>
"id", then the SQL column names in schema order. -
.count ⇒ Integer
The number of rows in the table.
-
.create(attrs = {}) ⇒ Article
Builds a record and saves it.
-
.find(id) ⇒ Article
The record with this id.
-
.find_by(conditions) ⇒ Article?
The first record matching the conditions (by id), or nil.
-
.first ⇒ Article?
The record with the lowest id.
-
.last ⇒ Article?
The record with the highest id.
-
.limit(n) ⇒ ArticleRelation
-
.order(order) ⇒ ArticleRelation
-
.order_sql(sql) ⇒ ArticleRelation
-
.table_name ⇒ String
The table,
"articles". -
.where(conditions) ⇒ ArticleRelation
Instance Method Summary collapse
-
#assign_attributes(attrs) ⇒ self
#write_attribute for each pair.
-
#comments ⇒ Array<Comment>
has_many, from the foreign keycomments.article_id: this article’s comments, by id. -
#initialize(attrs = {}) ⇒ Article
constructor
A new, unsaved record.
-
#model_name ⇒ String
"Article". -
#read_attribute(name) ⇒ Object?
The value of a column by name, the way templates read it.
-
#write_attribute(name, value) ⇒ nil
Sets a column by name, casting the value to the column’s type.
Methods inherited from Cybertrain::Model
#==, after_create, after_destroy, after_save, after_update, #as_json, #attributes, before_create, before_destroy, before_save, before_update, before_validation, #destroy, #errors, #new_record?, #persisted?, #reload, #save, #save!, #to_json, #to_param, #update, #valid?, validates
Constructor Details
#initialize(attrs = {}) ⇒ Article
A new, unsaved record. Keys may be Strings or Symbols; values are cast
to the column types; unknown keys and id are ignored.
155 |
# File 'docs/api/stubs/generated_app.rb', line 155 def initialize(attrs = {}); end |
Instance Attribute Details
#body ⇒ String?
The body column (t.text "body").
165 166 167 |
# File 'docs/api/stubs/generated_app.rb', line 165 def body @body end |
#created_at ⇒ Time?
Set by the first save when nil.
170 171 172 |
# File 'docs/api/stubs/generated_app.rb', line 170 def created_at @created_at end |
#title ⇒ String?
The title column (t.string "title").
160 161 162 |
# File 'docs/api/stubs/generated_app.rb', line 160 def title @title end |
#updated_at ⇒ Time?
Set by every save.
175 176 177 |
# File 'docs/api/stubs/generated_app.rb', line 175 def updated_at @updated_at end |
Class Method Details
.all ⇒ ArticleRelation
A relation over the whole table; every query starts here or at one of the shortcuts below. Nothing runs until rows are needed.
73 |
# File 'docs/api/stubs/generated_app.rb', line 73 def self.all = ArticleRelation.new("articles") |
.column_names ⇒ Array<String>
Returns "id", then the SQL column names in schema order.
65 |
# File 'docs/api/stubs/generated_app.rb', line 65 def self.column_names = ["id", "title", "body", "created_at", "updated_at"] |
.count ⇒ Integer
Returns the number of rows in the table.
129 |
# File 'docs/api/stubs/generated_app.rb', line 129 def self.count = all.count |
.create(attrs = {}) ⇒ Article
Builds a record and saves it. Returns the record whether or not it was
saved: check persisted? or
errors. There is no create!.
140 141 142 143 144 |
# File 'docs/api/stubs/generated_app.rb', line 140 def self.create(attrs = {}) rec = Article.new(attrs) rec.save rec end |
.find(id) ⇒ Article
The record with this id.
109 |
# File 'docs/api/stubs/generated_app.rb', line 109 def self.find(id) = all.find(id) |
.find_by(conditions) ⇒ Article?
The first record matching the conditions (by id), or nil.
117 |
# File 'docs/api/stubs/generated_app.rb', line 117 def self.find_by(conditions) = all.find_by(conditions) |
.first ⇒ Article?
Returns the record with the lowest id.
121 |
# File 'docs/api/stubs/generated_app.rb', line 121 def self.first = all.first |
.last ⇒ Article?
Returns the record with the highest id.
125 |
# File 'docs/api/stubs/generated_app.rb', line 125 def self.last = all.last |
.limit(n) ⇒ ArticleRelation
99 |
# File 'docs/api/stubs/generated_app.rb', line 99 def self.limit(n) = all.limit(n) |
.order(order) ⇒ ArticleRelation
87 |
# File 'docs/api/stubs/generated_app.rb', line 87 def self.order(order) = all.order(order) |
.order_sql(sql) ⇒ ArticleRelation
93 |
# File 'docs/api/stubs/generated_app.rb', line 93 def self.order_sql(sql) = all.order_sql(sql) |
.table_name ⇒ String
Returns the table, "articles".
61 |
# File 'docs/api/stubs/generated_app.rb', line 61 def self.table_name = "articles" |
.where(conditions) ⇒ ArticleRelation
81 |
# File 'docs/api/stubs/generated_app.rb', line 81 def self.where(conditions) = all.where(conditions) |
Instance Method Details
#assign_attributes(attrs) ⇒ self
#write_attribute for each pair.
199 |
# File 'docs/api/stubs/generated_app.rb', line 199 def assign_attributes(attrs); end |
#comments ⇒ Array<Comment>
has_many, from the foreign key comments.article_id: this article’s
comments, by id. A fresh query on every call; it returns an Array, so
filter with Comment.where instead when you need more than all of them.
When two foreign keys of one table point here, each reader is named
<table>_as_<key stem> (messages_as_sender).
210 |
# File 'docs/api/stubs/generated_app.rb', line 210 def comments = CommentRelation.new("comments").where(article_id: @id).to_a |
#model_name ⇒ String
Returns "Article".
179 |
# File 'docs/api/stubs/generated_app.rb', line 179 def model_name = "Article" |
#read_attribute(name) ⇒ Object?
The value of a column by name, the way templates read it.
185 |
# File 'docs/api/stubs/generated_app.rb', line 185 def read_attribute(name); end |
#write_attribute(name, value) ⇒ nil
Sets a column by name, casting the value to the column’s type. Unknown
names and :id are ignored.
193 |
# File 'docs/api/stubs/generated_app.rb', line 193 def write_attribute(name, value); end |