-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_examples.rb
More file actions
55 lines (36 loc) · 867 Bytes
/
db_examples.rb
File metadata and controls
55 lines (36 loc) · 867 Bytes
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
require 'mongoid'
require 'json'
Mongoid.load!("mongoid.yml", :development)
class User
include Mongoid::Document
field :name
field :email
end
### Create a new user
u1 = User.new(:name => "Tom", :email => 'tom@example.com')
# get the properties
u1.name
u1.email
# change the name
u1.name = "Tom Close"
# check the name has changed
u1.name
# save the user to the database
u1.save
# TODO: create another two users
### Using the database
# ====================
# To find out about what's in the database, we use methods of User
# How many users are there currently in the database
User.count
# find the first user
u2 = User.first
# check the properties
u2.name
u2.email
# find a specific user
u3 = User.find_by(:name => "Tom Close")
# pull all users out of the database
users = User.all
# print out their names
users.each {|u| puts u.name}