71
71
72
72
```
73
73
User.all
74
- User.first
74
+ User.first[()]
75
+ User.take[()]
75
76
User.find(2)
76
77
User.find_by_age(18) # deprecated
77
78
User.find_by(age: 18)
78
79
User.find_or_create_by(email: 'ololol@mail.ru')
80
+ User.find_each / find_in_batches
79
81
80
82
User.limit(10)
81
83
User.offset(10)
@@ -91,10 +93,13 @@ scope.reorder(:email).all
91
93
User.limit(4).to_sql
92
94
User.limit(4).unscoped.to_sql
93
95
User.limit(4).unscoped.none.to_sql
96
+ User.limit(4).ids
94
97
95
98
```
96
99
100
+
97
101
```
102
+ User.order(age: :desc)
98
103
User.order(:age).reverse_order
99
104
User.limit(5).select(:name)
100
105
User.limit(5).pluck(:name)
@@ -109,8 +114,10 @@ Hash[User.group(:age, :id).having('age > 18').select('COUNT(*) as number, age as
109
114
110
115
```
111
116
User.where(age: 18)
117
+ User.where.not(age:18)
112
118
User.where(age: 18).where(password: '1234')
113
119
User.where(age: 18, email: 'lol@gmail.com')
120
+ User.where(age: 18).exists?
114
121
age=18; User.where('age >= #{age}');
115
122
User.where('age >= ?', 18)
116
123
Usere.where('age >= ? OR email ILIKE ?, 18, 'test@gmail.com')
@@ -159,19 +166,99 @@ end
159
166
160
167
```
161
168
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
+
162
207
163
208
## Relations
164
209
210
+ * http://guides.rubyonrails.org/association_basics.html
211
+ * belongs_to
212
+ * has_one
213
+ * has_many
214
+ * has_many throught:
165
215
* (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
+
166
220
167
- ## STI
168
221
169
222
## Polymorphic associations
170
223
(association_name)_ type - Stores the type for polymorphic associations.
171
224
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
172
258
173
259
## Serialization
174
260
261
+
175
262
## Optimistic lock
176
263
177
264
lock_version - Adds optimistic locking to a model.
0 commit comments