You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docusaurus/docs/tutorial-ruby-couchbase-orm/02-installation.md
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Before installing Couchbase ORM, ensure that you have the following prerequisite
8
8
9
9
1.**Ruby**: Couchbase ORM requires Ruby version 2.7 or higher. You can check your Ruby version by running the following command in your terminal:
10
10
11
-
```
11
+
```sh
12
12
ruby -v
13
13
```
14
14
@@ -20,7 +20,7 @@ Before installing Couchbase ORM, ensure that you have the following prerequisite
20
20
21
21
3.**Bundler** (optional): Bundler is a dependency management tool for Ruby. While not strictly required, it is recommended to use Bundler to manage your Ruby project's dependencies. You can install Bundler by running the following command:
22
22
23
-
```
23
+
```sh
24
24
gem install bundler
25
25
```
26
26
@@ -75,17 +75,19 @@ To use Couchbase ORM in your Ruby application, you need to configure the connect
Replace the values for `connection_string`, `bucket`, `username`, and `password` with your actual Couchbase Server connection details.
86
86
87
87
Couchbase ORM will automatically load the configuration based on the current Rails environment.
88
88
89
+
You can also set the connection details using environment variables in the production environment to avoid storing sensitive information in the source code.
90
+
89
91
2. **Non-Rails Applications**:
90
92
91
93
For non-Rails applications, you can configure the connection settings programmatically. Add the following code to your application's initialization file or before you start using Couchbase ORM:
The `where` method allows you to specify conditions to filter the records based on attribute values. You can chain multiple `where` clauses together to build more complex queries.
You can also chain multiple `order` clauses to sort by multiple attributes.
114
75
115
76
@@ -121,39 +82,25 @@ The `pluck` method allows you to retrieve specific attributes from the matched r
121
82
122
83
```ruby
123
84
# Pluck names of all authors
124
-
puts"\nPluck names:"
125
85
putsAuthor.order(:name).pluck(:name).inspect
126
86
```
127
87
128
-
Output
129
-
```
130
-
Pluck names:
131
-
["Alice Brown", "Jane Smith", "John Doe"]
132
-
```
133
-
134
88
## 4.5. Destroying Records
135
89
136
90
To delete multiple records that match specific conditions, you can use the `.each(&:destroy)` method. It deletes the records from the database and returns the number of records deleted.
137
91
92
+
-`:destroy`: Deletes a single record.
93
+
-`delete_all`: Deletes all records that match the specified conditions.
Be cautious when using `.each(&:destroy)` as it permanently deletes the matched records from the database.
157
104
158
105
These are just a few examples of the querying capabilities provided by CouchbaseOrm. You can combine these methods in various ways to construct complex and specific queries based on your application's requirements.
Copy file name to clipboardExpand all lines: docusaurus/docs/tutorial-ruby-couchbase-orm/05-persistence.md
+10-24Lines changed: 10 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,21 +13,17 @@ class Task < CouchbaseOrm::Base
13
13
attribute :completed, :boolean, default:false
14
14
end
15
15
16
-
task1 =Task.new(title:'Task 1', description:'Description of Task 1')
17
-
task1.save
18
-
puts"Task 1 created with id: #{task1.id}, #{task1.inspect}"
16
+
task =Task.new(title:'Task 1', description:'Description of Task 1')
17
+
task.save
19
18
```
20
19
21
20
Alternatively, you can use the `create` method to create a new record in a single step:
22
21
23
22
```ruby
24
-
task1 =Task.create(title:'Task 1', description:'Description of Task 1')
25
-
```
26
-
Output of the above code:
27
-
```
28
-
Task 1 created with id: task-1-tLJM721QY, #<Task id: "task-1-tLJM721QY", title: "Task 1", description: "Description of Task 1", completed: false>
23
+
task =Task.create(title:'Task 1', description:'Description of Task 1')
29
24
```
30
25
26
+
31
27
The `create` method instantiates a new instance of the model, sets the attributes, and saves it to the database.
32
28
33
29
## 5.2. Saving Records
@@ -36,16 +32,12 @@ The `save` method is used to persist a record to the database, whether it's a ne
36
32
37
33
```ruby
38
34
# Update an existing task
39
-
task2 =Task.create(title:'Task 2', description:'Description of Task 2')
40
-
task2.description ='Updated description of Task 2'
41
-
task2.save
42
-
puts"Task 2 updated with id: #{task2.id}, #{task2.inspect}"
35
+
task =Task.create(title:'Task 2', description:'Description of Task 2')
36
+
task.description ='Updated description of Task 2'
37
+
task.save
43
38
```
44
39
45
-
Output of the above code:
46
-
```
47
-
Task 2 updated with id: task-1-tLJM895Xq, #<Task id: "task-1-tLJM895Xq", title: "Task 2", description: "Updated description of Task 2", completed: false>
48
-
```
40
+
49
41
50
42
If the record is new (i.e., it doesn't have an ID), `save` will create a new document in Couchbase Server. If the record already exists, `save` will update the existing document with the modified attributes.
51
43
@@ -55,14 +47,8 @@ To update an existing record, you can modify its attributes and then call the `s
55
47
56
48
```ruby
57
49
# Update specific fields of a task
58
-
task3 =Task.create(title:'Task 3', description:'Description of Task 3')
59
-
task3.update(description:'Updated description of Task 3', completed:true)
60
-
puts"Task 3 updated with id: #{task3.id}, #{task3.inspect}"
61
-
```
62
-
63
-
Output of the above code:
64
-
```
65
-
Task 3 updated with id: task-1-tLJMA6cBp, #<Task id: "task-1-tLJMA6cBp", title: "Task 3", description: "Updated description of Task 3", completed: true>
50
+
task =Task.create(title:'Task 3', description:'Description of Task 3')
51
+
task.update(description:'Updated description of Task 3', completed:true)
66
52
```
67
53
68
54
CouchbaseOrm automatically tracks the changes made to the attributes and updates only the modified fields in the database.
0 commit comments