-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotes: How the CRUD works
55 lines (49 loc) · 1.28 KB
/
Notes: How the CRUD works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# v is a variable that references the table entry
# ExampleTable is a database table with row entries that have ids, users, and statuses
v = ExampleTable.find(3)
v[:id] = 3
v.id = 3
#CREATE
#add an entry to ExampleTable, which has a column called "status"
v = ExampleTable.new
v.user = "fred"
v.status = "status message here"
v.save
#OR
v = ExampleTable.new(:status => "hi!", :user => "fred")
v.save
#OR
ExampleTable.create(:status => "hi!", :user => "fred")
#READ
#find the third entry in ExampleTable
ExampleTable.find(3)
#other read methods
ExampleTable.find(3,4,5)
ExampleTable.first
ExampleTable.last
ExampleTable.all
ExampleTable.count
ExampleTable.order(:user) #All ordered by user
ExampleTable.limit(10) #only 10 tweets
ExampleTable.where(:user => "fred") #only tweets by fred
ExampleTable.where(:user => "fred").order(:user).limit(10) #method chaining
#UPDATE
#update and save an entry on ExampleTable
v = ExampleTable.find(3)
v.user = "fred"
v.save
#OR
v = ExampleTable.find(3)
v.attributes = {:status => "hi!", :user => "fred"}
v.save
#OR
v = ExampleTable.find(3)
v.update_atttributes(:status => "hi!", :user => "fred") #updates and saves in one command
#DESTROY
#destroy an entry in ExampleTable
v = ExampleTable.find(3)
v.destroy
#OR
ExampleTable.find(3).destroy
#OR
ExampleTable.destroy_all