@@ -4,7 +4,7 @@ Create new records in the db, and play with models using irb.
4
4
## Start Rails Console
5
5
From root of application
6
6
```
7
- rails console
7
+ rails console
8
8
```
9
9
You may need to specify the environment (this will select the database you are working with)
10
10
```
@@ -26,7 +26,7 @@ You can also create a new record with values filled in during creation
26
26
subject = Subject.new(:name => "Subject 1", :position => 1, :visible => true)
27
27
```
28
28
29
- To save the record,
29
+ To save the record,
30
30
```
31
31
subject.save
32
32
```
@@ -115,22 +115,22 @@ Subject.where(:visible => true).order("position ASC")
115
115
```
116
116
117
117
Condition types to be included in where()
118
- ` where ` method
118
+ ` where ` method
119
119
1 . Hash as seen above ... ` Subject.where(:visible => true) `
120
120
- Does not support OR, LIKE, < or >
121
121
- Only supports equality, range, and subset checking
122
122
2 . Strings (Unsafe for variables)
123
123
3 . Array (rails can render out any SQL injection before executing, rendering them harmless).
124
124
- ` Subject.where(["name = ? visible = true", "First Subject"]) `
125
125
- "` First Subject ` replaces ` ? ` "
126
-
126
+
127
127
This method is stringable, so you can have multiple ` where ` methods added to a query...
128
128
```
129
129
Subject.where(:name => "kinsey").where(["name = ?"], "kinsey").first
130
130
```
131
131
132
132
### ORDER
133
- You can order by any column; alphabetically, chronologically, ascending
133
+ You can order by any column; alphabetically, chronologically, ascending
134
134
* Should specify` table_name ` and ` column_name `
135
135
* ASC
136
136
* DESC
@@ -145,4 +145,18 @@ Where to start when limiting results
145
145
146
146
```
147
147
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
+ ```
0 commit comments