Associations
All Futures supports embedded associations via the embeds_one / embeds_many / embedded_in macros provided by Active Entity, extended with a persistence layer: associated All Futures models are saved as their own Redis records, linked by a foreign key.
class Government < AllFutures::Base
embeds_many :spies, dependent: :destroy, autosave: true
end
class Spy < AllFutures::Base
attribute :government_id
attribute :codename, :string
embedded_in :government
end
government = Government.new
spy = government.spies.build(codename: "alpha")
government.save
spy.persisted? # => true
spy.government_id # => government.idLazy loading after find
Like Active Record, find does not eager-load children. The first time you touch an association on a persisted parent, All Futures loads the members from a Redis SET index (Government:<id>:spies) and hydrates the proxy:
Children saved on their own (with the foreign key set) are added to the same index, so they show up when the parent is loaded later.
Options
foreign_key:— the attribute on the child that stores the owner's id. Defaults to the association name plus_idonembedded_in(for examplegovernment_id), and to the owner's singular model name plus_idonembeds_one/embeds_many. The child must declare this attribute, orAllFutures::MissingForeignKeyErroris raised on save.autosave:— whentrue, saving the owner also saves loaded children that have unsaved changes.dependent:— what happens to children when the owner is destroyed::destroy— children are destroyed, running their callbacks:delete— children are removed from Redis without callbacks:nullify— children survive, with their foreign key set tonil:restrict_with_exception— raisesAllFutures::DeleteRestrictionErrorif any children exist:restrict_with_error— aborts the destroy (returnsfalse) and adds an error to the owner's:base
Nested attributes
accepts_nested_attributes_for works much like Active Record's:
Known limitations
Children saved through their owner skip version creation (they are saved
without_versioning), so a versioned child only accrues versions when saved directly.The Redis SET index is an implementation detail; do not rely on its key names outside All Futures.
Last updated