Comment on page
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.
class Example < AllFutures::Base
attribute :name, :string
attribute :age, :integer, default: 21
Let's use our new
Examplemodel to respond to a page request.
First, create an instance and assign it to an instance variable in the controller action:
class ExampleController < ApplicationController
@example = Example.create
Emit the instance id as a data attribute on every element which can update your model.
Name: <input type="text" data-id="<%= @example.id %>" data-reflex="input->Example#name" /><br/>
Age: <input type="text" data-id="<%= @example.id %>" data-reflex="input->Example#age" />
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
findthe correct All Futures object and make changes to it.
The following methods both find a record and update it, using StimulusReflex:
class ExampleReflex < ApplicationReflex
example = Example.find(element.dataset.id)
example.name = element.value
Example.find(element.dataset.id).update age: element.value
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
idof 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
saveoperation is now required to persist changes.
Example.new # no values set, and not yet persisted
Example.new name: "Steve" # not yet persisted
example = Example.new name: "Steve"
example.save # now it's persisted
puts example.id # you'll need the id to access this instance later
Example.create # no values set; persisted but no way to access the id
example_id = Example.create(name: "Bob").id # winning
createis exactly like
new, except that the model instance is persisted to Redis before it returns. If you want to set your own
create, it's important to specify an
Example.create name: "Bob", id: 555
example = Example.find(example_id)
An incomplete list of attribute/method names that you shouldn't use as attributes:
If you are experiencing strange behaviour with an attribute, consider using the
respond_to?method the see if there is a naming conflict.
The root node in your locale YAML is