Time for action — including a version

Let's go versioning:

class Delta
include Mongoid::Document
include Mongoid::Versioning
field :name, type: String
end

Let's see it in action:

irb> a = Delta.create
=> #<Delta _id: 4f22f748fed0eb9e6e000003, _type: nil, version: 1, name: nil>
irb> a.name = "First"
=> "First"
irb> a.save
=> true
irb> a
=> #<Delta _id: 4f22f748fed0eb9e6e000003, _type: nil, version: 2, name: "First">
irb> a.name = "Second"
=> "Second"
irb> a.save
=> true
irb> a
=> #<Delta _id: 4f22f748fed0eb9e6e000003, _type: nil, version: 3, name: "Second">
irb> a.revise!
=> true
irb> a
=> #<Delta _id: 4f22f748fed0eb9e6e000003, _type: nil, version: 4, name: "Second">

What just happened?

When we included the Versioning module:

  • A field called version gets added to the document with default value 1
  • A cyclic relation called versions gets added

The model is now configured to update the version every time the object is saved. When it's created the first time, notice that the version number is set:

irb> a
=> #<Delta _id: 4f22f748fed0eb9e6e000003, _type: nil,
version: 1,

name: nil>

Every time, the object is saved, the version number is incremented and the versioned attributes (that is, all the fields in the document) get saved inside the versions embedded object's array and the version is incremented.

If we want to update the version without any changes, we can use the revise! method.

Tip

Some more fancy stuff with versioning

If you want to save the document but don't want to version it, use the versionless method. This temporarily disables versioning, for example, object.versionless(&:save).

If you want to see changes made to the object, use the :previous_changes method.

If you want to see the versioned objects, use the :versions method.

Notice, that we mentioned cyclic relationship. We saw this earlier in the embedded relations. For versioning, we need exactly one parent and many child documents of the same class embedded in it!

Pop quiz — dancing with Mongoid models

  1. Which of the following is the incorrect way of accessing the title field of the Book model?
    • a. Book.first.title.
    • b. Book.first[:title].
    • c. Book.first.read_attribute(:title).
    • d. Book.first.get_title.
  2. When a field is localized, how is that field stored in the database?
    • a. As an embedded object.
    • b. As an array.
    • c. As a hash.
    • d. As a comma-separated string.
  3. What does the cascaded_callbacks option do?
    • a. Enables callback invocation on the embedded object.
    • b. Cascaded deletes the callbacks in children.
    • c. Enables callback invocation for parent object.
    • d. Disables callback invocation on the embedded object.
  4. What would recursively_embeds_many in the Author model not do?
    • a. Add a cyclic embeds_many relation for Author.
    • b. It creates an array of embedded objects called child_authors.
    • c. Add a field called parent_author in the Author model.
    • d. Adds a field called author_count in the Author model.
  5. Why do we need to specify the embedded_in relation in the embedded Model?
    • a. Mongoid needs to index this embedded object.
    • b. All documents are Mongoid::Document. This is the only way Mongoid knows that the document is embedded in another document.
    • c. Mongoid needs to store this in the embedded collection.
    • d. When Mongoid::EmbeddedDocument is specified, we do not need this relation, otherwise we need it.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.115.237