Add tomdoc
This commit is contained in:
parent
2aa5ad8379
commit
f295e6ce80
107
rails-models.md
107
rails-models.md
|
@ -3,11 +3,84 @@ title: Rails Models
|
||||||
layout: default
|
layout: default
|
||||||
---
|
---
|
||||||
|
|
||||||
### Generating models
|
### [Query methods](http://devdocs.io/rails/activerecord/querymethods#method-i-order)
|
||||||
|
|
||||||
|
```rb
|
||||||
|
items = Model
|
||||||
|
.where(first_name: "Harvey")
|
||||||
|
|
||||||
|
.order(:title)
|
||||||
|
.order(title: :desc)
|
||||||
|
.order("title DESC")
|
||||||
|
|
||||||
|
.reorder(:title) # discards other .orders
|
||||||
|
.rewhere(...)
|
||||||
|
|
||||||
|
.limit(2)
|
||||||
|
.offset(1)
|
||||||
|
.uniq
|
||||||
|
```
|
||||||
|
|
||||||
|
### [FinderMethods](http://devdocs.io/rails/activerecord/findermethods)
|
||||||
|
|
||||||
|
```rb
|
||||||
|
item = Model.find(id)
|
||||||
|
item = Model.find_by_email(email)
|
||||||
|
item = Model.where(email: email).first
|
||||||
|
|
||||||
|
Model
|
||||||
|
.exists?(5)
|
||||||
|
.exists?(name: "David")
|
||||||
|
|
||||||
|
.first
|
||||||
|
.last
|
||||||
|
.find_nth(4, [offset])
|
||||||
|
```
|
||||||
|
|
||||||
|
### [Persistence](http://devdocs.io/rails/activerecord/persistence)
|
||||||
|
|
||||||
|
```rb
|
||||||
|
item.new_record?
|
||||||
|
item.persisted?
|
||||||
|
item.destroyed?
|
||||||
|
|
||||||
|
item.serialize_hash
|
||||||
|
|
||||||
|
item.save
|
||||||
|
item.save! # Same as above, but raises an Exception
|
||||||
|
|
||||||
|
item.update name: "John"
|
||||||
|
item.update!
|
||||||
|
|
||||||
|
item.update_column :name, "John" # skips validations and callbacks
|
||||||
|
item.update_columns name: "John"
|
||||||
|
item.update_columns! name: "John"
|
||||||
|
|
||||||
|
item.touch # updates :updated_at
|
||||||
|
item.touch :published_at
|
||||||
|
|
||||||
|
item.valid?
|
||||||
|
item.invalid?
|
||||||
|
|
||||||
|
item.destroy
|
||||||
|
item.delete # skips callbacks
|
||||||
|
```
|
||||||
|
|
||||||
|
```rb
|
||||||
|
Model.create # Same an #new then #save
|
||||||
|
Model.create! # Same as above, but raises an Exception
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sorting
|
||||||
|
|
||||||
|
|
||||||
|
Generating
|
||||||
|
----------
|
||||||
|
|
||||||
$ rails g model User
|
$ rails g model User
|
||||||
|
|
||||||
### Associations
|
Associations
|
||||||
|
------------
|
||||||
|
|
||||||
belongs_to
|
belongs_to
|
||||||
has_one
|
has_one
|
||||||
|
@ -192,37 +265,13 @@ Validation
|
||||||
|
|
||||||
record.errors[:name].any?
|
record.errors[:name].any?
|
||||||
|
|
||||||
API
|
Other API
|
||||||
---
|
---------
|
||||||
|
|
||||||
items = Model.find_by_email(email)
|
### Callbacks
|
||||||
items = Model.where(first_name: "Harvey")
|
|
||||||
|
|
||||||
item = Model.find(id)
|
|
||||||
|
|
||||||
item.serialize_hash
|
|
||||||
item.new_record?
|
|
||||||
|
|
||||||
item.create # Same an #new then #save
|
|
||||||
item.create! # Same as above, but raises an Exception
|
|
||||||
|
|
||||||
item.save
|
|
||||||
item.save! # Same as above, but raises an Exception
|
|
||||||
|
|
||||||
item.update
|
|
||||||
item.update_attributes
|
|
||||||
item.update_attributes!
|
|
||||||
|
|
||||||
item.valid?
|
|
||||||
item.invalid?
|
|
||||||
|
|
||||||
* [Guides: callbacks](http://guides.rubyonrails.org/active_record_validations_callbacks.html)
|
* [Guides: callbacks](http://guides.rubyonrails.org/active_record_validations_callbacks.html)
|
||||||
|
|
||||||
### Sorting
|
|
||||||
|
|
||||||
posts.order(:title)
|
|
||||||
posts.order("title DESC")
|
|
||||||
|
|
||||||
### Mass updates
|
### Mass updates
|
||||||
|
|
||||||
# Updates person id 15
|
# Updates person id 15
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
---
|
||||||
|
title: Tomdoc
|
||||||
|
---
|
||||||
|
|
||||||
|
```
|
||||||
|
# Public: Duplicate some text an arbitrary number of times.
|
||||||
|
#
|
||||||
|
# text - The String to be duplicated.
|
||||||
|
# count - The Integer number of times to duplicate the text.
|
||||||
|
#
|
||||||
|
# Examples
|
||||||
|
#
|
||||||
|
# multiplex('Tom', 4)
|
||||||
|
# # => 'TomTomTomTom'
|
||||||
|
#
|
||||||
|
# Returns the duplicated String.
|
||||||
|
def multiplex(text, count)
|
||||||
|
text * count
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Tags
|
||||||
|
|
||||||
|
```
|
||||||
|
Deprecated
|
||||||
|
Internal
|
||||||
|
Public
|
||||||
|
```
|
||||||
|
|
||||||
|
### Options
|
||||||
|
|
||||||
|
```
|
||||||
|
# options - The Hash options used to refine the selection (default: {}):
|
||||||
|
# :color - The String color to restrict by (optional).
|
||||||
|
# :weight - The Float weight to restrict by. The weight should
|
||||||
|
# be specified in grams (optional).
|
||||||
|
```
|
||||||
|
|
||||||
|
### Yields
|
||||||
|
|
||||||
|
```
|
||||||
|
Yields the Integer index of the iteration.
|
||||||
|
Returns the duplicated String.
|
||||||
|
Returns nothing.
|
||||||
|
Raises Errno::ENOENT if the file can't be found.
|
||||||
|
Returns something else and this is a wrapped
|
||||||
|
multi-line comment.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Signatures
|
||||||
|
|
||||||
|
```
|
||||||
|
# Signature
|
||||||
|
#
|
||||||
|
# find_by_<field>[_and_<field>...](args)
|
||||||
|
#
|
||||||
|
```
|
||||||
|
|
||||||
|
See [tomdoc.org](http://tomdoc.org/).
|
Loading…
Reference in New Issue