Skip to content

Commit 6973e43

Browse files
committed
Adding and using named scopes to models
1 parent ec80f49 commit 6973e43

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

Tutorial_Docs/Rails_Console.MD

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Create new records in the db, and play with models using irb.
44
## Start Rails Console
55
From root of application
66
```
7-
rails console
7+
rails console
88
```
99
You may need to specify the environment (this will select the database you are working with)
1010
```
@@ -26,7 +26,7 @@ You can also create a new record with values filled in during creation
2626
subject = Subject.new(:name => "Subject 1", :position => 1, :visible => true)
2727
```
2828

29-
To save the record,
29+
To save the record,
3030
```
3131
subject.save
3232
```
@@ -115,22 +115,22 @@ Subject.where(:visible => true).order("position ASC")
115115
```
116116

117117
Condition types to be included in where()
118-
`where` method
118+
`where` method
119119
1. Hash as seen above ... `Subject.where(:visible => true)`
120120
- Does not support OR, LIKE, < or >
121121
- Only supports equality, range, and subset checking
122122
2. Strings (Unsafe for variables)
123123
3. Array (rails can render out any SQL injection before executing, rendering them harmless).
124124
- `Subject.where(["name = ? visible = true", "First Subject"])`
125125
- "`First Subject` replaces `?`"
126-
126+
127127
This method is stringable, so you can have multiple `where` methods added to a query...
128128
```
129129
Subject.where(:name => "kinsey").where(["name = ?"], "kinsey").first
130130
```
131131

132132
### ORDER
133-
You can order by any column; alphabetically, chronologically, ascending
133+
You can order by any column; alphabetically, chronologically, ascending
134134
* Should specify` table_name` and `column_name`
135135
* ASC
136136
* DESC
@@ -145,4 +145,18 @@ Where to start when limiting results
145145

146146
```
147147
subject = Subject.where(:visible => true).order("subjects.position ASC").limit(20).offset(40)
148-
```
148+
```
149+
150+
## Using named scopes in Rails Console
151+
You can declare named scopes in you [model]('../app/models/subject.rb'), and then run them in the rails console.
152+
* You must restart rails console after added a named scope.
153+
154+
### Usage:
155+
In rails console
156+
```
157+
Subject.visible
158+
```
159+
160+
```
161+
Subject.search("Third")
162+
```

app/models/subject.rb

+11
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
class Subject < ApplicationRecord
2+
3+
# Lamda is read at execution time, which is important for dates.
4+
scope :visible, lambda { where(:visible => true) }
5+
# Stabby lambda syntax is subtly different, but can also be used.
6+
scope :invisible, -> { where(:visible => false) }
7+
scope :sorted, lambda { order("subjects.position ASC") }
8+
scope :newest_first, lambda { order("subjects.created_at DESC") }
9+
scope :search, lambda { |query|
10+
where(["name LIKE ?", "%${query}%"])
11+
}
12+
213
end

0 commit comments

Comments
 (0)