Please enable JavaScript.
Coggle requires JavaScript to display documents.
:silhouettes: Active Record (:black_right_pointing_triangle_with_double…
:silhouettes: Active Record
:recycle:
Associations
tips tricks wars
Controlling caching
= The cache is even shared across methods. Can use atc. entity.subentity.
reload
.empty?
Avoiding name collisions
=
Updating the schema
= you must contoll your
model
,
schema
and
database
(foreign keys atc.. ). You got different migrations for new table and exists table
oin-tables-for-has-and-belongs-to-many-associations
= you can specify your join table name by :join attr
Controlling association scope
= all entities must be in the same module!! You can hack it by
class_name:
attr
directional associations
= 's normal for associations to work in two directions (has_one / has_many + belongs_to). You do it very efective
types
belongs_to
=
one-to-one
connection with another model (
belongs to
). Table has
foreign key
. +
detail
has_one
=
one-to-one
connection with another model (
contains
) +
Detail
has_many
=
one-to-many
connection with another model +
detail
has_many :through
=
many-to-many
connection with another model.
through a third model
has_one :through
=
one-to-one
connection with another model
through third model
has_and_belongs_to_many
=
direct many-to-many
connection with another model, with no intervening model +
detail
:straight_ruler: The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity.
polymorphic accociations
= a model can belong to more than one other model, on a single association
self join
other
association callbacks
= hooks: before_add, after_remove atc..
association extensions
= add next functionality to entities. Extend base funct
Single table inheritence
= can use in ganerator as well
:house:
Basic
crud
references on - validation, callbacks, migration
:satellite:
Validations
others
Displaying Validation Errors in Views
Working with Validation Errors
Performing Custom Validations
common options
on = only on update, create atc..
message = specify the err mess
allow_blank is similar to nil (empyt val)
allow_nil = skip if is nil
save vs save!
functions on entity instance
.new_record? = is unique in database
.valid? = is valid (.invalid?)
.errors.messages = return list of errors
helpers
validates_each = for every item from group
validates_with = separe validations to another class :recycle:
acceptance
validates_associated :entity_name = call entity validations
confirmation: true = use eg. password, email atc..
exclusion: = not contains
format: reg validation
inclusion: = in set..
length = min, max, range atc..
numericality = only number
presence: true = required
uniqueness
:hammer_and_pick:
migrations
creating migration
rails generate migration
migrationName
model generators
= rails g model Product name:string description:text
Writting migration
execute direct sql
= you can use for hard update
Reverting Previous Migrations
running migration
base commands
rails db:migrate VERSION=20080906120000
rails db:migrate
rails db:migrate:up VERSION=20080906120000 (run specific migration)
rollback
= db:rollback || db:rollback STEP=3
db:migrate:redo STEP=3
= revert last three migrations
set up databae
= The rails
db:setup
task will create the database, load the schema and initialize it with the seed data.
db:reset
= drop database and set up again
run in different env
= rails db:migrate RAILS_ENV=test
Changing existing migration
= do not do it! call db:rollback and db:migrate
Dump db
= db/schema.rb. Do not contains triggers, functions and procedures
migration and seed data
= can set default data for database
:black_right_pointing_triangle_with_double_vertical_bar:
callbacks
= triggers on entities
Callback registration
List of existing callbacks
Creating object
Updating object
Destroying object
after find/initialize
these methods run callbacks
Skipping callbacks
relations callbacks
= run all callbacks in inheritences
Conditional callbacks
= run callback if..
if / unless
multiple conditions
separate logic into another class
transation callback
:question:
queries