summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-05-04 13:51:26 -0600
committermo khan <mo.khan@gmail.com>2020-05-04 13:51:26 -0600
commita0018e0919d2658fb488e2f4462a4e270fbe0ddb (patch)
tree6be2eee95d4e986926b9ed631d56ea1e81c2f0ed
parent0a2a83ed17ceb910a29596a11d7c309c3c010e7a (diff)
add some notes on scopesHEADmaster
-rw-r--r--README.md27
1 files changed, 25 insertions, 2 deletions
diff --git a/README.md b/README.md
index 7b12401..04ae13e 100644
--- a/README.md
+++ b/README.md
@@ -2900,7 +2900,7 @@ def change
end
```
-# [insert_all](https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all)
+* [insert_all](https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all)
```ruby
users = []
@@ -2954,7 +2954,7 @@ Rails.application.config.generators do |g|
end
```
-## rails transactions
+### rails transactions
```ruby
t.decimal :amount, precision: 10, scale: 2
@@ -2971,4 +2971,27 @@ class Account
end
```
+### Rails paritioning
+
Partition data using [pg_party](https://github.com/rkrage/pg_party) gem.
+
+### Rails Scopes
+
+```ruby
+
+# bad
+scope :active -> {
+ includes(:client)
+ .where(active: true)
+ .select { |project| project.client.active? }
+ .sort_by { |project| project.name.downcase }
+}
+
+# scopes should return an ActiveRecord::Relation
+# filter in the database not ruby
+# sort in the database not ruby
+
+# better
+scope :active, -> { where(active: true) .joins(:client) .merge(Client.active) }
+scope :ordered, -> { order('LOWER(name)') }
+```