Skip to content

Commit 1cc2ac1

Browse files
committed
Adds create/update via rails console
1 parent 54f2d96 commit 1cc2ac1

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,16 @@ You can go through the migrations sequentially by using VERSION=<timestamp> of w
194194
So if you only wanted to run the Create users migration, you'd enter
195195
```
196196
rake db:migrate VERSION=20160917170621
197-
```
197+
```
198+
199+
## Rails Console
200+
From the root of your application, you can perform tasks using your application
201+
to models and the database through `rails console`
202+
```
203+
rails console
204+
```
205+
To specify enironment:
206+
```
207+
rails console c9
208+
```
209+
For more info, check out [Rails Console]('./Tutorial_Docs/Rails_Console.MD').

Tutorial_Docs/Rails_Console.MD

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Rails Console
2+
Create new records in the db, and play with models using irb.
3+
4+
## Start Rails Console
5+
From root of application
6+
```
7+
rails console
8+
```
9+
You may need to specify the environment (this will select the database you are working with)
10+
```
11+
rails c c9
12+
```
13+
14+
## Creating Records
15+
```
16+
subject = Subject.new
17+
```
18+
19+
To see if the subject is already saved in db
20+
```
21+
subject.new_record?
22+
```
23+
24+
You can also create a new record with values filled in during creation
25+
```
26+
subject = Subject.new(:name => "Subject 1", :position => 1, :visible => true)
27+
```
28+
29+
To save the record,
30+
```
31+
subject.save
32+
```
33+
34+
You can also add logic to the rails console
35+
```
36+
if subject.save
37+
puts "Saved"
38+
else
39+
puts "Not Saved"
40+
end
41+
```
42+
43+
### Create()
44+
```
45+
subject = Subject.create(:name => "Subject 2", :position => 2)
46+
```
47+
48+
## Updating Records
49+
Find subject by id
50+
```
51+
subject = Subject.find(1)
52+
```
53+
You can then update the fields either with `mass assignment`
54+
```
55+
subject.name = "New Name"
56+
```
57+
Or
58+
```
59+
subject.update_attributes(:name => "New Name")
60+
```

0 commit comments

Comments
 (0)