% Ruby-on-Rails method cheat sheets
Optimised for pandoc flavour, but works more or less on Github flavour.
The table summarises the instance methods of Relations, which can be used as class methods of Methods,
and their returned values (self means a Relation instance, even when it is run as a class method of a Model).
Several instance methods of Model instances that do similar operations are also included.
| Relation-methods | Model-instance | self | Array | Model | Bool | Else | Note |
|---|---|---|---|---|---|---|---|
all |
✔ | ||||||
any? |
✔ | ||||||
average(Sym) |
Object | ||||||
count |
Integer | ||||||
create(Hash) |
✔ | ||||||
create_with(Hash) |
✔ | ||||||
destroy |
✔ | ||||||
destroy_all |
✔ | ||||||
distinct(set=true) |
✔ | ||||||
eager_load(*Syms) |
✔ | ||||||
entries |
✔ | ||||||
exists?(*Int) |
✔ | ||||||
extending(*modu, &bl) |
✔ | ||||||
explain |
String | ||||||
find(*Int) |
✔ | ✔ | |||||
find_by(Hash) |
✔ | ||||||
find_by!(Hash) |
✔ | ||||||
find_each(&b) |
✔ | ||||||
find_or_create_by(Hash,&b) |
✔ | ||||||
find_or_initialize_by |
✔ | ||||||
find_by_sql(SQL,*Arr) |
✔ | ||||||
first(Int) |
✔ | ✔ | |||||
first_or_initialize |
✔ | ||||||
forty_two |
✔ | ||||||
from(Table,sub-index) |
✔ | ||||||
group(String) |
✔ | ||||||
having(String) |
✔ | ||||||
id |
Integer | ||||||
ids |
✔ | ||||||
includes(*Obj) |
✔ | ||||||
joins(*Obj) |
✔ | ||||||
last(Int) |
✔ | ||||||
left_outer_joins |
✔ | ||||||
limit(Int) |
✔ | ||||||
lock(true) |
✔ | ||||||
lock!(true) |
✔ | ✔ | Row-based | ||||
locked? |
✔ | ||||||
locking_enabled? |
✔ | ✔ | |||||
lock_optimistically |
✔ | ||||||
lock_optimistically? |
✔ | ||||||
with_lock(&bloc) |
Row-based | ||||||
many? |
✔ | ||||||
maximum(Sym) |
Object | ||||||
minimum(Sym) |
Object | ||||||
new(*Hash) |
✔ | ||||||
none |
✔ | (Empty) | |||||
offset(Int) |
✔ | ||||||
only(*Sym) |
✔ | ||||||
or(other) |
✔ | ||||||
order(Sym,Hs) |
✔ | ||||||
pluck(Sym) |
✔ | ||||||
preload(*Sym) |
✔ | ||||||
readonly |
✔ | ||||||
references(*tb_names) |
✔ | ||||||
reorder(*) |
✔ | ||||||
reverse_order |
✔ | ||||||
rewhere(*) |
✔ | ||||||
select(*Sym) |
✔ | ||||||
select_all(Str) |
✔ | ||||||
unscope(*Sym) |
✔ | ||||||
update(Hash) |
✔ | ||||||
update_all(String) |
|||||||
take |
✔ | ||||||
take(Int) |
✔ | ||||||
transaction(&bloc) |
|||||||
where(Hash/*Arr) |
✔ |
: Summary of Relation instance methods
Suppose the DB table has a column name called mycol.
| Model methods | self | Hash | Array | Bool | Else | Note |
|---|---|---|---|---|---|---|
attribute_changed?(Sym) |
✔ | |||||
attribute_changed_in_place?(Sym) |
✔ | |||||
attribute_in_database(Sym) |
Object | |||||
attribute_names |
✔ | |||||
attributes |
✔ | {str => Object} |
||||
attributes= |
||||||
attributes_in_database |
✔ | Alias: changed_attributes |
||||
changed |
✔ | changed_attributes.keys |
||||
changed? |
✔ | Any unsaved changes exist? | ||||
changed_attributes |
✔ | original values of unsaved | ||||
changed_attribute_names_to_save |
✔ | Alias: changed |
||||
changes |
✔ | {str=>[bef, aft]} unsaved |
||||
changes_to_save |
✔ | Alias: changes |
||||
destroyed? |
✔ | |||||
errors |
Errors | cf errors.details[:mycol] |
||||
has_attribute?(Sym) |
✔ | |||||
has_changes_to_save? |
✔ | Alias: changed? |
||||
new_record? |
✔ | |||||
persisted? |
✔ | neither new nor destroyed? | ||||
previous_changes |
✔ | Before/After save | ||||
restore_attributes |
Undo the unsaved changes | |||||
saved_changes |
✔ | of Hash=>Array, just saved | ||||
saved_changes? |
✔ | !saved_changes.empty? |
||||
created_at |
Time | |||||
id |
Integer | |||||
type |
||||||
updated_at |
Time | |||||
mycol |
Object | |||||
mycol= |
Object | |||||
mycol? |
✔ | |||||
mycol_before_last_save |
Object | |||||
mycol_before_type_cast |
Object | |||||
mycol_came_from_user? |
✔ | |||||
mycol_change |
✔ | |||||
mycol_change_to_be_saved |
✔ | |||||
mycol_changed? |
✔ | |||||
mycol_in_database |
Object | |||||
mycol_previous_change |
✔ | |||||
mycol_previously_changed? |
✔ | |||||
mycol_was |
Object | |||||
mycol_will_change! |
||||||
restore_mycol! |
||||||
saved_change_to_mycol |
✔ | |||||
saved_change_to_mycol? |
✔ | |||||
will_save_change_to_mycol? |
✔ | |||||
becomes(Klass) |
✔ | |||||
delete |
✔ | Avoids callbacks | ||||
destroy |
✔ | Returned frozen | ||||
decrement(Sym, by=1) |
✔ | Skip validation | ||||
increment(Sym, by=1) |
✔ | Skip validation | ||||
reload |
✔ | |||||
save(validate: true) |
✔ | |||||
save!(touch: true) |
✔ | Exception if fails | ||||
toggle(Sym) |
✔ | Toggles Boolean value | ||||
touch(*Syms, time: nil) |
||||||
update(Hash) |
✔ | m.a=X; m.save |
||||
update!(Hash) |
✔ | m.a=X; m.save! |
||||
update_attribute(Sym,Obj) |
No validation but callbacks | |||||
update_attributes(Hash) |
||||||
update_attributes!(Hash) |
||||||
update_columns(Hash) |
Direct SQL call | |||||
valid? |
✔ | !invalid? |
: Model insntance methods and its returns