Class: Article

Inherits:
Cybertrain::Model show all
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

Attributes inherited from Cybertrain::Model

#id

Class methods (finders) collapse

Instance Method Summary collapse

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.

Examples:

Article.new(title: "Hello")
Article.new(params.require(:article).permit(:title, :body))

Parameters:

  • attrs (Hash{String, Symbol => Object}) (defaults to: {})


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").

Returns:

  • (String, nil)


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.

Returns:

  • (Time, 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").

Returns:

  • (String, nil)


160
161
162
# File 'docs/api/stubs/generated_app.rb', line 160

def title
  @title
end

#updated_at ⇒ Time?

Set by every save.

Returns:

  • (Time, nil)


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.

Examples:

@articles = Article.all.to_a

Returns:



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.

Returns:

  • (Array<String>) —

    "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.

Returns:

  • (Integer) —

    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!.

Examples:

article = Article.create(title: "Hello", body: "A body long enough")
article.persisted?  # => true

Parameters:

  • attrs (Hash{String, Symbol => Object}) (defaults to: {})

Returns:



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.

Examples:

@article = Article.find(params[:id])

Parameters:

  • id (Integer, String) —

    a String (such as params[:id]) is compared as SQLite compares it with the integer column

Returns:

Raises:



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.

Examples:

Article.find_by(title: "Hello")

Parameters:

  • conditions (Hash{Symbol => Object})

Returns:



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.

Returns:

  • (Article, nil) —

    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.

Returns:

  • (Article, nil) —

    the record with the highest id



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

def self.last = all.last

.limit(n) ⇒ ArticleRelation

Parameters:

  • n (Integer)

Returns:

See Also:



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

def self.limit(n) = all.limit(n)

.order(order) ⇒ ArticleRelation

Parameters:

  • order (String, Symbol) —

    column [ASC|DESC], comma-separated

Returns:

See Also:



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

def self.order(order) = all.order(order)

.order_sql(sql) ⇒ ArticleRelation

Parameters:

  • sql (String) —

    raw ORDER BY text; never request data

Returns:

See Also:



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".

Returns:

  • (String) —

    the table, "articles"



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

def self.table_name = "articles"

.where(conditions) ⇒ ArticleRelation

Examples:

Article.where(title: "Hello").first

Parameters:

  • conditions (Hash{Symbol => Object})

Returns:

See Also:



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.

Parameters:

  • attrs (Hash{String, Symbol => Object})

Returns:

  • (self)


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).

Examples:

<% @article.comments.each do |comment| %>

Returns:



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".

Returns:

  • (String) —

    "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.

Parameters:

  • name (Symbol) —

    the SQL column name (or :id); a String gives nil

Returns:

  • (Object, nil) —

    nil for an unknown name



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.

Parameters:

  • name (Symbol)
  • value (Object)

Returns:

  • (nil)


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

def write_attribute(name, value); end