Usage
Working with All Futures is intentionally very similar to working with Active Record, and most of the same methods will work. You will create a class and define the scopes, validations, callbacks and instance methods you need.
All Futures models persist to Redis instead of your relational database. Place All Futures classes in app/models
, alongside your Active Record models.
Hello World
For this example, we'll use StimulusReflex to send updates to the server when the user enters data into either of two text input elements.
The most visible difference between an Active Record model and an All Futures model is that instead of migrations and a schema, you need to declare your attributes in the class:
Let's use our new Example
model to respond to a page request.
First, create an instance and assign it to an instance variable in the controller action:
Emit the instance id as a data attribute on every element which can update your model.
Since all attributes are gathered and sent to the server during a Reflex operation, it's easy to retrieve the instance id from the Reflex element accessor and use it to find
the correct All Futures object and make changes to it.
The following methods both find a record and update it, using StimulusReflex:
You can now update your instance across multiple calls or requests, regardless of whether the user refreshes or navigates away from the page. So long as you have the id
of the instance you need, you can access it until your Redis cache expiry policy purges it at some point in the distant future.
All Futures v1 persisted the attributes every time you set the value of an attribute using bracket notation. This behavior has been removed. An explicit save
operation is now required to persist changes.
Creating model instances
There are two ways to create an All Futures class instance: new
and create
. Both methods accept an optional Hash of attributes:
create
is exactly like new
, except that the model instance is persisted to Redis before it returns. If you want to set your own id
when calling create
, it's important to specify an id
value.
Finding model instances
Retrieving an instance later just requires passing an id
to the find
method. Numeric values will be converted to String type for performing the lookup.
In All Futures, id
is not an attribute. It's not treated as data.
Reserved words
An incomplete list of attribute/method names that you shouldn't use as attributes:
id
created_at
updated_at
If you are experiencing strange behaviour with an attribute, consider using the respond_to?
method the see if there is a naming conflict.
Internationalization
I18n in All Futures is similar to Active Record.
The root node in your locale YAML is allfutures
instead of activerecord
.
Last updated