Skip to content

Commit a35d730

Browse files
committed
work on notes for 9
1 parent b5805f6 commit a35d730

File tree

4 files changed

+123
-14
lines changed

4 files changed

+123
-14
lines changed

materials/9/app/app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class User < ActiveRecord::Base
2+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateUsers < ActiveRecord::Migration
2+
def change
3+
create_table :users do |t|
4+
t.string :email
5+
t.string :name
6+
t.integer :age
7+
t.string :password
8+
9+
t.timestamps
10+
end
11+
end
12+
end

materials/9/app/db/schema.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# encoding: UTF-8
2+
# This file is auto-generated from the current state of the database. Instead
3+
# of editing this file, please use the migrations feature of Active Record to
4+
# incrementally modify your database, and then regenerate this schema definition.
5+
#
6+
# Note that this schema.rb definition is the authoritative source for your
7+
# database schema. If you need to create the application database on another
8+
# system, you should be using db:schema:load, not running all the migrations
9+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
10+
# you'll amass, the slower it'll run and the greater likelihood for issues).
11+
#
12+
# It's strongly recommended that you check this file into your version control system.
13+
14+
ActiveRecord::Schema.define(version: 20140216101712) do
15+
16+
# These are extensions that must be enabled in order to support this database
17+
enable_extension "plpgsql"
18+
19+
create_table "users", force: true do |t|
20+
t.string "email"
21+
t.string "name"
22+
t.integer "age"
23+
t.string "password"
24+
t.datetime "created_at"
25+
t.datetime "updated_at"
26+
end
27+
28+
end

notes/9.md

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ create_table :users do |t|
2626
t.string :name
2727
t.string :password
2828
t.string :age
29+
t.boolean :not_nerd
2930
end
3031
3132
create_table :people do |t|
@@ -38,34 +39,34 @@ end
3839
User.create email: Faker::Internet.email,
3940
name: Faker::Name.name,
4041
password: '123456',
41-
age: rand(12..90)
42+
age: rand(12..90),
43+
not_nerd: true
4244
end
4345
4446
```
4547

4648
### List of methods
47-
* bind
4849
* create_with
4950
* eager_load
5051
* extending
5152
* from
52-
* group
53+
* group +
5354
* having
5455
* includes
5556
* joins
56-
* limit
57+
* limit +
5758
* lock
58-
* none
59-
* offset
60-
* order
59+
* none +
60+
* offset +
61+
* order +
6162
* preload
6263
* readonly
6364
* references
6465
* reorder +
65-
* reverse_order
66-
* select
67-
* distinct
68-
* uniq
66+
* reverse_order +
67+
* select +
68+
* distinct +
69+
* uniq +
6970
* where
7071

7172
```
@@ -77,9 +78,8 @@ User.find_by(age: 18)
7778
User.find_or_create_by(email: 'ololol@mail.ru')
7879
7980
User.limit(10)
80-
User.limit(10).offset(10)
81-
User.order(:age).limit(10)
82-
User.order('age DESC')
81+
User.offset(10)
82+
User.order(:age)
8383
```
8484

8585
```
@@ -88,11 +88,78 @@ scope = scope.offset(10)
8888
scope = scope.order(:age)
8989
scope.reorder(:email).all
9090
91+
User.limit(4).to_sql
92+
User.limit(4).unscoped.to_sql
93+
User.limit(4).unscoped.none.to_sql
94+
95+
```
96+
97+
```
98+
User.order(:age).reverse_order
99+
User.limit(5).select(:name)
100+
User.limit(5).pluck(:name)
101+
User.select(:age).distinct
102+
User.select(:age).uniq
103+
User.group(:age).select('COUNT(*) as number, age as age')
104+
Hash[User.group(:age).select('COUNT(*) as number, age as age').map {|u| [u.age, u.number]}]
105+
Hash[User.group(:age, :id).having('age > 18').map {|u| [u.age, u.number]}]
106+
Hash[User.group(:age, :id).having('age > 18').select('COUNT(*) as number, age as age').map {|u| [u.age, u.number]}]
107+
108+
```
109+
110+
```
111+
User.where(age: 18)
112+
User.where(age: 18).where(password: '1234')
113+
User.where(age: 18, email: 'lol@gmail.com')
114+
age=18; User.where('age >= #{age}');
115+
User.where('age >= ?', 18)
116+
Usere.where('age >= ? OR email ILIKE ?, 18, 'test@gmail.com')
117+
User.where('age < :age, create_at >= :date', {age: 18, date: Time.zone.now})
118+
91119
```
92120

93121

94122
## Validations
95123

124+
125+
* model.errors
126+
* model.valid? || model.invalid?
127+
* shebang methods
128+
* skipping validations
129+
130+
131+
```
132+
class User < ActiveRecord::Base
133+
validates_presense_of :name
134+
validates :name, presence: true
135+
validates :name, absence: true
136+
validates :not_nerd, acceptance: true
137+
# validates :email, confirmation: true
138+
validates :email, format: { with: Devise.regexp}
139+
validates :not_nerd, inclusion: { in:[true, false]}
140+
validates :name, length: { minimum: 5, maximum: 120 }
141+
validates :age, numericality: { only_integer: true, greater_than: 0 }
142+
validates :name, uniqueness: { scope: :email, case_sensitive: false }
143+
validates :name, presence: true, if:
144+
145+
end
146+
147+
class GoodnessValidator < ActiveModel::Validator
148+
def validate(record)
149+
if record.name =~ "Evil"
150+
record.errors[:base] << "This person is evil"
151+
end
152+
end
153+
end
154+
155+
class User < ActiveRecord::Base
156+
validates_with GoodnessValidator
157+
end
158+
159+
160+
```
161+
162+
96163
## Relations
97164

98165
* (table_name)_count - Used to cac* he the number of belonging objects on associations.

0 commit comments

Comments
 (0)