Class: Comment

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

Overview

The other side of Article#comments: what spin run gen writes for a comments table with t.references :article (an article_id column and a foreign key to articles). It has the same finders as Article, through CommentRelation.

Instance Attribute Summary collapse

Attributes inherited from Cybertrain::Model

#id

Class Method Summary 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, belongs_to, #destroy, encrypts, #errors, has_many, has_one, model_concern_insert_at, model_concern_insert_done, #new_record?, #persisted?, #reload, #save, #save!, scope, #to_json, #to_param, transaction, #update, #update_columns, upsert, #valid?, validate, validates, validates_foreign_key

Instance Attribute Details

#article_id ⇒ Integer

The foreign key; 0 until set (references columns are null: false).

Returns:



850
851
852
# File 'docs/api/stubs/generated_app.rb', line 850

def article_id
  @article_id
end

#body ⇒ String?

Returns:

  • (String, nil)


845
846
847
# File 'docs/api/stubs/generated_app.rb', line 845

def body
  @body
end

#commenter ⇒ String?

Returns:

  • (String, nil)


841
842
843
# File 'docs/api/stubs/generated_app.rb', line 841

def commenter
  @commenter
end

Class Method Details

.all ⇒ CommentRelation

Returns:



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

def self.all = CommentRelation.new("comments")

.where(cond = nil, *binds) ⇒ CommentRelation

Parameters:

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

Returns:



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

def self.where(cond = nil, *binds) = all.where(cond, *binds)

Instance Method Details

#article ⇒ Article?

belongs_to, from the foreign key article_id (or a belongs_to :article line): the article, or nil when there is none. The answer is kept for the article_id it was read for: the next call with the same key runs no query, a changed key reads again, and reload forgets it. When a column already has the name, the reader is <stem>_as_<column> (author_as_author_id).

Returns:



860
861
862
863
864
865
866
867
# File 'docs/api/stubs/generated_app.rb', line 860

def article
  return @article_record if @article_loaded && @article_loaded_key == @article_id

  @article_record = Article.find_by(id: @article_id)
  @article_loaded = true
  @article_loaded_key = @article_id
  @article_record
end