Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several options you have. Here are a couple things to keep in mind:</p> <ul> <li>Mongoid has a versioning plugin where you can keep track of versions of a document</li> <li>You can create an embedded document to store notes/changes on a model. Use an observer to add a note when certain things happen. You can tie this note to the document version if you'd like.</li> </ul> <p>I have a case where I'm using an embedded Note object to track the state and progression of an order. Here's a rough outline of what I did:</p> <pre><code>class Order include Mongoid::Document include Mongoid::Paranoia include Mongoid::Timestamps embeds_many :notes, as: :notable # fields end class Note include Mongoid::Document include Mongoid::Timestamps field :message field :state field :author #(I can add notes to any Model through :notable) embedded_in :notable, polymorphic: true end </code></pre> <p>Then I created an observer to track state changes in Order:</p> <pre><code>class OrderObserver &lt; Mongoid::Observer def after_transition(order, transition) order.notes.build(state: transition.to) end end </code></pre> <p><code>after_transition</code> is a callback that the state machine plugin provides. If you don't care about integrating a state machine, you can just use Mongoid-provided callbacks like <code>after_save</code>, <code>after_update</code>, <code>around_update</code>, etc.</p> <p>Each time I transition through the states of an Order, I get a new timestamped note that records the history of each transition. I've left a lot of implementation details out, but it's working well for me so far.</p> <p>Links:</p> <ul> <li><a href="http://mongoid.org/docs/extras.html" rel="nofollow">mongoid extras</a></li> <li><a href="http://github.com/pluginaweek/state_machine" rel="nofollow">state_machine</a></li> <li><a href="http://mongoid.org/docs/callbacks/observers.html" rel="nofollow">mongoid observers</a> - what callbacks Mongoid provides for Observers</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload