Skip to content

Commit c2ccf94

Browse files
committed
add some final notes for tomorrows lecture
1 parent a35d730 commit c2ccf94

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

notes/9.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ end
7171

7272
```
7373
User.all
74-
User.first
74+
User.first[()]
75+
User.take[()]
7576
User.find(2)
7677
User.find_by_age(18) # deprecated
7778
User.find_by(age: 18)
7879
User.find_or_create_by(email: 'ololol@mail.ru')
80+
User.find_each / find_in_batches
7981
8082
User.limit(10)
8183
User.offset(10)
@@ -91,10 +93,13 @@ scope.reorder(:email).all
9193
User.limit(4).to_sql
9294
User.limit(4).unscoped.to_sql
9395
User.limit(4).unscoped.none.to_sql
96+
User.limit(4).ids
9497
9598
```
9699

100+
97101
```
102+
User.order(age: :desc)
98103
User.order(:age).reverse_order
99104
User.limit(5).select(:name)
100105
User.limit(5).pluck(:name)
@@ -109,8 +114,10 @@ Hash[User.group(:age, :id).having('age > 18').select('COUNT(*) as number, age as
109114

110115
```
111116
User.where(age: 18)
117+
User.where.not(age:18)
112118
User.where(age: 18).where(password: '1234')
113119
User.where(age: 18, email: 'lol@gmail.com')
120+
User.where(age: 18).exists?
114121
age=18; User.where('age >= #{age}');
115122
User.where('age >= ?', 18)
116123
Usere.where('age >= ? OR email ILIKE ?, 18, 'test@gmail.com')
@@ -159,19 +166,99 @@ end
159166
160167
```
161168

169+
## Scopes
170+
171+
```
172+
class User < ActiveRecord::Base
173+
 default_scope { where("removed_at IS NULL") }
174+
scope :created_before, ->(time) { where("created_at < ?", time) }
175+
end
176+
177+
```
178+
179+
## Callbacks
180+
http://guides.rubyonrails.org/active_record_callbacks.html
181+
182+
## Creating an Object
183+
* before_validation
184+
* after_validation
185+
* before_save
186+
* around_save
187+
* before_create
188+
* around_create
189+
* after_create
190+
* after_save
191+
192+
### Updating an Object
193+
* before_validation
194+
* after_validation
195+
* before_save
196+
* around_save
197+
* before_update
198+
* around_update
199+
* after_update
200+
* after_save
201+
202+
### Destroying an Object
203+
* before_destroy
204+
* around_destroy
205+
* after_destroy
206+
162207

163208
## Relations
164209

210+
* http://guides.rubyonrails.org/association_basics.html
211+
* belongs_to
212+
* has_one
213+
* has_many
214+
* has_many throught:
165215
* (table_name)_count - Used to cac* he the number of belonging objects on associations.
216+
* people vs profile
217+
* slavers vs people
218+
* people vs toilets
219+
166220

167-
## STI
168221

169222
## Polymorphic associations
170223
(association_name)_type - Stores the type for polymorphic associations.
171224

225+
```
226+
class Picture < ActiveRecord::Base
227+
belongs_to :imageable, polymorphic: true
228+
end
229+
230+
class Employee < ActiveRecord::Base
231+
has_many :pictures, as: :imageable
232+
end
233+
234+
class Product < ActiveRecord::Base
235+
has_many :pictures, as: :imageable
236+
end
237+
238+
class CreatePictures < ActiveRecord::Migration
239+
def change
240+
create_table :pictures do |t|
241+
t.string :name
242+
t.integer :imageable_id
243+
t.string :imageable_type
244+
t.timestamp
245+
OR
246+
t.references :imageable, polymorphic: true
247+
end
248+
end
249+
end
250+
251+
252+
```
253+
254+
## STI
255+
256+
class Admin < User
257+
end
172258

173259
## Serialization
174260

261+
175262
## Optimistic lock
176263

177264
lock_version - Adds optimistic locking to a model.

0 commit comments

Comments
 (0)